Skip to content

Commit d1cd298

Browse files
committed
백제완: [PG] 250135 [PCCP 기출문제] 3번 / 아날로그 시계_241205
1 parent fc64e2a commit d1cd298

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Programmers/Level2/JW_250135.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Solution {
2+
public int solution(int h1, int m1, int s1, int h2, int m2, int s2) {
3+
int answer = 0;
4+
int now = h1 * 3600 + m1 * 60 + s1;
5+
int end = h2 * 3600 + m2 * 60 + s2;
6+
// 시작부터 겹치고 시작할 경우
7+
if (now == 0 || now == 43200)
8+
answer++;
9+
while (now < end) {
10+
double[] nowAngles = calculateAndgles(now);
11+
double[] nextAngles = calculateAndgles(now + 1);
12+
boolean isHourMatched = chkHour(nowAngles, nextAngles);
13+
boolean isMinMatched = chkMin(nowAngles, nextAngles);
14+
if (isHourMatched)
15+
answer++;
16+
if (isMinMatched)
17+
answer++;
18+
// 둘 다 겹칠 경우에는 한번만 체크
19+
if (isHourMatched && isMinMatched)
20+
if (nextAngles[0] == nextAngles[1])
21+
answer--;
22+
now++;
23+
}
24+
return answer;
25+
}
26+
27+
private double[] calculateAndgles(int time) {
28+
double[] angles = new double[3];
29+
double h = time / 3600, m = time % 3600 / 60, s = time % 3600 % 60;
30+
// 시침의 현재 각도
31+
angles[0] = (h % 12) * (360d / 12) + m * (360d / 12 / 60) + s * (360d / 12 / 3600);
32+
// 분침의 현재 각도
33+
angles[1] = m * (360d / 60) + s * (360d / 60 / 60);
34+
// 초침의 현재 각도
35+
angles[2] = s * (360d / 60);
36+
return angles;
37+
}
38+
39+
// 시침 겹침 확인
40+
private boolean chkHour(double[] now, double[] next) {
41+
double nowSec = now[2], nextSec = next[2];
42+
// 1초의 움직임 안에 시침이 포함되는가
43+
if (nowSec < now[0] && next[0] <= nextSec)
44+
return true;
45+
// 59초의 값은 따로 계산 → 원형이기에 각도가 0으로 초기화되기 때문
46+
if (nowSec == 354d && 354d < now[0])
47+
return true;
48+
return false;
49+
}
50+
51+
// 분침 겹침 확인
52+
private boolean chkMin(double[] now, double[] next) {
53+
double nowSec = now[2], nextSec = next[2];
54+
// 1초의 움직임 안에 시침이 포함되는가
55+
if (nowSec < now[1] && next[1] <= nextSec)
56+
return true;
57+
// 59초의 값은 따로 계산 → 원형이기에 각도가 0으로 초기화되기 때문
58+
if (nowSec == 354d && 354d < now[1])
59+
return true;
60+
return false;
61+
}
62+
}

0 commit comments

Comments
 (0)