[백준 30544] Cuckoo! Cuckoo! (C#, C++) - soo:bak
작성일 :
문제 링크
설명
시작 시각에서 뻐꾸기 울음 횟수를 세어 N번째 울음이 끝난 시각을 출력하는 문제입니다.
접근법
현재 시각이 정각이면 시간만큼, 15분이나 30분 또는 45분이면 1번 울립니다.
이후 시작 시각부터 분을 1씩 증가시키며 울음 횟수를 차감하고, 목표 횟수에 도달하는 순간의 시각을 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
class Program {
static void Main() {
var time = Console.ReadLine()!.Split(':');
var h = int.Parse(time[0]);
var m = int.Parse(time[1]);
var n = int.Parse(Console.ReadLine()!);
while (true) {
var chimes = 0;
if (m == 0) chimes = (h == 12) ? 12 : h;
else if (m == 15 || m == 30 || m == 45) chimes = 1;
if (chimes > 0) {
if (n <= chimes) {
Console.WriteLine($"{h:D2}:{m:D2}");
return;
}
n -= chimes;
}
m++;
if (m == 60) {
m = 0;
h++;
if (h == 13) h = 1;
}
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string t; cin >> t;
int h = stoi(t.substr(0, 2));
int m = stoi(t.substr(3, 2));
int n; cin >> n;
while (true) {
int chimes = 0;
if (m == 0) chimes = (h == 12) ? 12 : h;
else if (m == 15 || m == 30 || m == 45) chimes = 1;
if (chimes > 0) {
if (n <= chimes) {
cout << setw(2) << setfill('0') << h << ":"
<< setw(2) << setfill('0') << m << "\n";
return 0;
}
n -= chimes;
}
m++;
if (m == 60) {
m = 0;
h++;
if (h == 13) h = 1;
}
}
}