๐ŸŒž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๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.