๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 17047_Titlovi

๋ฟŒ์•ผ._. 2025. 12. 12. 12:20
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/17047)

< Titlovi >

 

๋ฌธ์ œ ํ’€์ด 

 

์‹œ๊ฐ„์„ ์ผ์ •๋Ÿ‰ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ์•ž, ๋’ค๋กœ ์ด๋™ํ•˜๋ฉด์„œ ๋ฐ€๋ฆฌ์ดˆ, ์ดˆ, ๋ถ„, ์‹œ์— ๋งž๊ฒŒ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.

 

my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class _17047_ { // Titlovi

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;

		String str = "";

		ArrayList<ArrayList<String>> list = new ArrayList<>();

		while (true) {
			ArrayList<String> temp = new ArrayList<>();

			temp.add(bf.readLine());

			st = new StringTokenizer(bf.readLine());

			String start = st.nextToken();
			st.nextToken();
			String end = st.nextToken();

			temp.add(start);
			temp.add(end);

			while (!(str = bf.readLine()).isEmpty()) {
				if (str.equals("#")) {
					break;
				}
				temp.add(str);
			}
			list.add(temp);
			if (str.equals("#")) {
				break;
			}
		}
		int t = Integer.parseInt(bf.readLine());

		for (int i = 0; i < list.size(); i++) {
			ArrayList<String> temp = list.get(i);

			bw.write(temp.get(0) + "\n");

			for (int j = 1; j < 3; j++) {
				int ttt = Integer.parseInt(temp.get(j).split(",")[1]);
				int h = Integer.parseInt((temp.get(j).split(",")[0]).split(":")[0]);
				int m = Integer.parseInt((temp.get(j).split(",")[0]).split(":")[1]);
				int s = Integer.parseInt((temp.get(j).split(",")[0]).split(":")[2]);
				int cnt = 0;

				if (t < 0) {
					ttt += t;
					if (ttt < 0) {
						cnt = Math.abs(ttt) / 1000 + 1;
						ttt += 1000 * cnt;
						s -= cnt;

						if (s < 0) {
							cnt = Math.abs(s) / 60 + 1;
							s += 60 * cnt;
							m -= cnt;

							if (m < 0) {
								cnt = Math.abs(m) / 60 + 1;
								m += 60 * cnt;
								h -= cnt;
							}
						}
					}
				} else {
					ttt += t;
					if (ttt >= 1000) {
						cnt = ttt / 1000;
						ttt -= 1000 * cnt;
						s += cnt;

						if (s >= 60) {
							cnt = s / 60;
							s -= 60 * cnt;
							m += cnt;

							if (m >= 60) {
								cnt = m / 60;
								m -= 60 * cnt;
								h += cnt;
							}
						}
					}

				}
				bw.write(String.format("%02d", h) + ":" + String.format("%02d", m) + ":" + String.format("%02d", s)
						+ "," + String.format("%03d", ttt));
				if (j == 1) {
					bw.write(" --> ");
				} else {
					bw.write("\n");
				}
			}
			for (int j = 3; j < temp.size(); j++) {
				bw.write(temp.get(j) + "\n");
			}
			if (i < list.size() - 1) {
				bw.write("\n");
			}
		}
		bw.write("#");
		bw.flush();
	}
}
๋ณ€์ˆ˜)
str : ์ž…๋ ฅ๊ฐ’
list : ๋ธ”๋ก ๊ฐ’๋“ค์„ ๋ชจ์•„๋‘” ArrayList
temp : ๊ฐ ๋ธ”๋ก ๊ฐ’
start, end : ์‹œ์ž‘ ์‹œ๊ฐ„, ์ข…๋ฃŒ ์‹œ๊ฐ„
t : ๋”ํ•ด์•ผ ํ•˜๋Š” ๋ฐ€๋ฆฌ์ดˆ 
ttt, h, m, s : ๋ฐ€๋ฆฌ์ดˆ, ์‹œ, ๋ถ„, ์ดˆ 

 

#๊ฐ€ ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€ ๊ฐ ๋ธ”๋ก์„ ์ž…๋ ฅ๋ฐ›์•„ temp์— ์ €์žฅํ•œ ๋’ค list์— ์ €์žฅํ•œ๋‹ค. ๋”ํ•ด์•ผ ํ•˜๋Š” ๋ฐ€๋ฆฌ์ดˆ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. list๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๊ฐ ๋ธ”๋ก์˜ ๊ฐ’์„ ํƒ์ƒ‰ํ•œ๋‹ค. ๋ธ”๋ก ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์‹œ์ž‘ ์‹œ๊ฐ„๊ณผ ์ข…๋ฃŒ ์‹œ๊ฐ„์— t๋งŒํผ ๋”ํ•œ๋‹ค. ์ด๋•Œ, ์Œ์ˆ˜๊ฐ’์ด ๋‚˜์˜ค๋ฉด ์ดˆ, ๋ถ„, ์‹œ์—์„œ ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ , 1000 ์ด์ƒ์ด ๋‚˜์˜ค๋ฉด ์ดˆ, ๋ถ„, ์‹œ์— ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•ด ์ค€๋‹ค. t๋งŒํผ ๋”ํ•œ ์‹œ์ž‘ ์‹œ๊ฐ„๊ณผ ์ข…๋ฃŒ ์‹œ๊ฐ„์„ ํ˜•์‹์— ๋งž๊ฒŒ ์ถœ๋ ฅํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์ž๋ง‰๊ณผ ๋นˆ ์ค„์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์—๋Š” #์„ ์ถœ๋ ฅํ•˜๋ฉฐ ์ข…๋ฃŒํ•œ๋‹ค. 



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 10331_Miscalculation  (0) 2025.12.10
[Baekjoon] 16652_Email Destruction  (0) 2025.12.09
[Baekjoon] 7318_Parencodings  (0) 2025.12.08
[Baekjoon] 33094_Diet Plan  (0) 2025.12.05
[Baekjoon] 10654_Cow Jog  (0) 2025.12.04