Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 13c2cb7

Browse files
committed
배수빈: [PG] 250135 아날로그 시계_241206
1 parent 4a9fc9b commit 13c2cb7

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

‎Programmers/Level2/SB_250135.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class SB_250135 {
2+
private static boolean overlapHour(Time cur, Time nxt) {
3+
if(cur.dh > cur.ds && nxt.dh <=nxt.ds) return true;
4+
return cur.ds == 354 && cur.dh > 354;
5+
}
6+
private static boolean overlapMin(Time cur, Time nxt) {
7+
if(cur.dm > cur.ds && nxt.dm <=nxt.ds) return true;
8+
return cur.ds == 354 && cur.dm > 354; // 초침이 정각으로 갈때 0도 처리
9+
}
10+
public static int solution(int h1, int m1, int s1, int h2, int m2, int s2) {
11+
int startTime = new Time(h1, m1, s1).getSecond();
12+
int endTime = new Time(h2, m2, s2).getSecond();
13+
14+
int cnt = 0;
15+
for (int i = startTime; i < endTime; i++) {
16+
Time cur = new Time(i);
17+
Time nxt = new Time(i + 1);
18+
19+
boolean isMinRing = overlapMin(cur, nxt);
20+
boolean isHourRing = overlapHour(cur, nxt);
21+
22+
if (isMinRing && isHourRing){ // 시침 분침 모두 겹쳤을때
23+
if (nxt.dh== nxt.dm) cnt++; // 시침분침의 위치가 같으면 한번만 카운트
24+
else cnt+=2;
25+
} else if (isMinRing || isHourRing) cnt++;
26+
}
27+
if (startTime==0 || startTime==43200) cnt++; // 예외처리: 처음에 시분초 모두 같이 있을 경우(정오, 정각)
28+
return cnt;
29+
}
30+
31+
static class Time{
32+
int h, m, s;
33+
double dh, dm, ds;
34+
35+
public Time(int h, int m, int s) {
36+
this.h = h;
37+
this.m = m;
38+
this.s = s;
39+
setDegrees();
40+
}
41+
42+
public Time(int s) {
43+
this.h = s / 3600;
44+
this.m = (s % 3600) / 60;
45+
this.s = (s % 3600) % 60;
46+
setDegrees();
47+
}
48+
49+
50+
private int getSecond() {
51+
return h * 3600 + m * 60 + s;
52+
}
53+
54+
private void setDegrees() {
55+
this.dh = (h % 12) * 30d + m * 0.5 + s * (0.5 / 60);
56+
this.dm = m * 6 + s * 0.1;
57+
this.ds = s * 6;
58+
}
59+
}
60+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /