๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง  ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][PCCP ๊ธฐ์ถœ๋ฌธ์ œ] 3๋ฒˆ / ์•„๋‚ ๋กœ๊ทธ ์‹œ๊ณ„ JSํ’€์ด

by megan07 2023. 12. 29.

๋ฌธ์ œ ๋งํฌ

PCCP 3๋ฒˆ

ํ’€์ด ๋ฐฉ๋ฒ•

์ƒ๊ฐ๋ณด๋‹ค ์‹œ๊ฐ„์ด ๊ฑธ๋ ค์„œ ๋‹ค๋ฅธ ๋ถ„์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ’€์ด๊ฐ€ ์ง๊ด€์ ์ด๊ณ  ์ดํ•ด๊ฐ€ ์‰ฌ์šด ๊ฒƒ ๊ฐ™์•„์š”

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

 

์‹œ์นจ, ์ดˆ์นจ๊ณผ ๋ถ„์นจ, ์ดˆ์นจ์ด ๊ฒน์ณค๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด 1์ดˆ๋งˆ๋‹ค ๊ฐ๋„๋ฅผ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํ˜„์žฌ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์ดˆ์นจ์˜ ๊ฐ๋„๊ฐ€ ์‹œ์นจ์˜ ๊ฐ๋„๋ณด๋‹ค ์ž‘๊ณ , 1์ดˆ ๋’ค์— ์‹œ์นจ์˜ ๊ฐ๋„๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด ๊ฒน์ณ์ง„ ๊ฒƒ์ด๋ฏ€๋กœ ์นด์šดํŠธ๋ฅผ 1 ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค ๋ถ„์นจ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.

๋‹ค๋งŒ 3๊ฐ€์ง€๊ฐ€ ๊ฒน์ณค์„ ๊ฒฝ์šฐ์—๋Š” 1๋ฒˆ๋งŒ ์นด์šดํŠธ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ 1์„ ๋นผ์ค๋‹ˆ๋‹ค.

1. ๋‚ด ํ’€์ด

function solution(h1, m1, s1, h2, m2, s2) {
  //์ดˆ๋‹น ์›€์ง์ด๋Š” ์ดˆ์นจ, ๋ถ„์นจ, ์‹œ์นจ ๊ฐ๋„
  const SECONDS_ANGLE = 360 / 60;
  const MINUTES_ANGLE = 360 / 60 / 60;
  const HOURS_ANGLE = 360 / 12 / 60 / 60;

  let cnt = 0;

  //์‹œ์ž‘ ์‹œ๊ฐ„ ์ดˆ๋‹จ์œ„
  let start_time = h1 * 60 * 60 + m1 * 60 + s1;

  //๋๋‚˜๋Š” ์‹œ๊ฐ„ ์ดˆ๋‹จ์œ„
  let end_time = h2 * 60 * 60 + m2 * 60 + s2;

  const start_hour_angle = (start_time * HOURS_ANGLE) % 360;
  const start_minute_angle = (start_time * MINUTES_ANGLE) % 360;
  const start_seconds_angle = (start_time * SECONDS_ANGLE) % 360;

  //์‹œ์ž‘ ์‹œ๊ฐ„์— ์ดˆ์นจ๊ณผ ์‹œ์นจ์ด ๊ฒน์ณ์žˆ์œผ๋ฉด ์นด์šดํŠธ
  if (start_seconds_angle === start_hour_angle) cnt++;
  //์‹œ์ž‘ ์‹œ๊ฐ„์— ์ดˆ์นจ๊ณผ ๋ถ„์นจ์ด ๊ฒน์ณ์žˆ์œผ๋ฉด ์นด์šดํŠธ
  if (start_seconds_angle === start_minute_angle) cnt++;
  //์‹œ์ž‘ ์‹œ๊ฐ„์— ์‹œ์นจ, ๋ถ„์นจ, ์ดˆ์นจ 3๊ฐ€์ง€๊ฐ€ ๊ฒน์ณ์žˆ๋Š” ๊ฒฝ์šฐ -1
  if (
    start_seconds_angle === start_hour_angle &&
    start_seconds_angle === start_minute_angle
  )
    cnt--;

  //1์ดˆ๋งˆ๋‹ค ๊ฒน์ณค๋Š”์ง€ ํ™•์ธ
  //ํ˜„์žฌ ์ดˆ์นจ์˜ ๊ฐ๋„๊ฐ€ ์‹œ์นจ, ๋ถ„์นจ ๊ฐ๋„๋ณด๋‹ค ์ž‘๋‹ค๊ฐ€ 1์ดˆ ๋’ค์— ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๊ฒน์ณ์ง„ ๊ฒƒ
  while (start_time < end_time) {
    //ํ˜„์žฌ ์‹œ๊ฐ„ ์‹œ์นจ, ๋ถ„์นจ, ์ดˆ์นจ ๊ฐ๋„
    const current_hours_angle = (start_time * HOURS_ANGLE) % 360;
    const current_minutes_angle = (start_time * MINUTES_ANGLE) % 360;
    const current_seconds_angle = (start_time * SECONDS_ANGLE) % 360;

    //1์ดˆ ๋’ค
    const next_time = start_time + 1;

    //1์ดˆ ๋’ค ์‹œ์นจ, ๋ถ„์นจ, ์ดˆ์นจ ๊ฐ๋„
    //360๋„์ผ ๋•Œ ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ํ•˜๋ฉด 0์ด ๋˜๋ฏ€๋กœ 0์ผ ๊ฒฝ์šฐ์—๋Š” 360์œผ๋กœ ์„ค์ •ํ•ด์คŒ
    const next_hours_angle = (next_time * HOURS_ANGLE) % 360 || 360;
    const next_minutes_angle = (next_time * MINUTES_ANGLE) % 360 || 360;
    const next_seconds_angle = (next_time * SECONDS_ANGLE) % 360 || 360;

    //์ดˆ์นจ์ด ์‹œ์นจ ๋’ค์— ์žˆ์—ˆ๋Š”๋ฐ 1์ดˆ ๋’ค์— ์‹œ์นจ ์•ž์œผ๋กœ ๊ฐ€๋ฉด ์นด์šดํŠธ+1
    if (
      current_seconds_angle < current_hours_angle &&
      next_seconds_angle >= next_hours_angle
    )
      cnt++;
    //์ดˆ์นจ์ด ๋ถ„์นจ ๋’ค์— ์žˆ์—ˆ๋Š”๋ฐ 1์ดˆ ๋’ค์— ๋ถ„์นจ ์•ž์œผ๋กœ ๊ฐ€๋ฉด ์นด์šดํŠธ+1
    if (
      current_seconds_angle < current_minutes_angle &&
      next_seconds_angle >= next_minutes_angle
    )
      cnt++;
    //1์ดˆ ๋’ค์— ์‹œ์นจ ๋ถ„์นจ ์ดˆ์นจ 3๊ฐœ๊ฐ€ ๊ฒน์น˜๋ฉด -1(0์‹œ, 12์‹œ)
    if (
      next_seconds_angle === next_hours_angle &&
      next_seconds_angle === next_minutes_angle
    )
      cnt--;

    //์‹œ์ž‘ ์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ
    start_time = next_time;
  }

  return cnt;
}