๐Algorithm/๐ฅBaekjoon
[Baekjoon] 26876_New Time
๋ฟ์ผ._.
2025. 5. 26. 12:25
๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/26876)
< New Time >
๋ฌธ์ ํ์ด
(๋ ๋ฒ์งธ ์ ๋ ฅ๋ ์๊ฐ - ์ฒซ ๋ฒ์งธ ์ ๋ ฅ๋ ์๊ฐ)์ ๊ณ์ฐํ ํ ๊ฐ๊ฐ ์ฐจ์ด ๊ฐ์ ๋ํ๋ค.
์ ๋ ฅ๊ฐ์ด ๋ค์๊ณผ ๊ฐ๋ค๋ฉด
09:09
21:21
์๊ฐ ์ฐจ๋ 12:12 ์ด๋ฏ๋ก 12+12 = 24๊ฐ ์ ๋ต์ด๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _26876_ { // New Time
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String a = bf.readLine();
String b = bf.readLine();
int time[] = new int[3];
time[0] = Integer.parseInt(a.split(":")[0]);
time[1] = Integer.parseInt(a.split(":")[1]);
int time2[] = new int[3];
time2[0] = Integer.parseInt(b.split(":")[0]);
time2[1] = Integer.parseInt(b.split(":")[1]);
int result = 0;
if (time[1] > time2[1]) {
time2[1] += 60;
time2[0] -= 1;
if (time2[0] < 0) {
time2[0] = 23;
}
}
result += time2[1] - time[1];
if (time[0] > time2[0]) {
time2[0] += 24;
}
result += time2[0] - time[0];
System.out.println(result);
}
}
๋ณ์)
a, b : ์ ๋ ฅ๊ฐ
time, time2 : ๊ฐ ์ ๋ ฅ๊ฐ์ split ํ ํ int ๊ฐ์ผ๋ก ์ ์ฅ
result : ์ต์ ๋ฒํผ ๋๋ฆ ํ์
์๊ฐ 2๊ฐ๋ฅผ ์ ๋ ฅ๋ฐ์ a, b์ ์ ์ฅํ๋ค. ":"์ ๊ธฐ์ค์ผ๋ก split ํ์ฌ int๋ก ํ๋ณํ ํ ๊ฐ์ time๊ณผ time2 ๋ฐฐ์ด์ ๊ฐ๊ฐ ์ ์ฅํ๋ค. ๋ถ, ์ ์์๋ก ์ฐจ์ด๊ฐ์ ๊ตฌํ ํ ํฉํ๋ค. ์ต์ข result๋ฅผ ์ถ๋ ฅํ๋ค.