๋ฌธ์ (์ถ์ฒ: 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 |