문제링크
https://www.acmicpc.net/problem/1340
문제
문빙이는 새해를 좋아한다. 하지만, 이제 새해는 너무 많이 남았다. 그래도 문빙이는 새해를 기다릴 것이다.
어느 날 문빙이는 잠에서 깨면서 스스로에게 물었다. “잠깐, 새해가 얼마나 남은거지?”
이 문제에 답하기 위해서 문빙이는 간단한 어플리케이션을 만들기로 했다. 연도 진행바라는 것인데, 이번 해가 얼마나 지났는지를 보여주는 것이다.
오늘 날짜가 주어진다. 이번 해가 몇%지났는지 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 Month DD, YYYY HH:MM과 같이 주어진다. Month는 현재 월이고, YYYY는 현재 연도이다. 숫자 네자리이다. DD, HH, MM은 모두 2자리 숫자이고, 현재 일, 시, 분이다.
Month는 January, February, March, April, May, June, July, August, September, October, November, December 중의 하나이고, 연도는 1800보다 크거나 같고, 2600보다 작거나 같다. 항상 올바른 날짜와 시간만 입력으로 주어진다.
출력
첫째 줄에 문제의 정답을 출력한다. 절대/상대 오차는 10-9까지 허용한다.
해석
날짜가 분까지 주어지기 때문에 현재까지의 시간을 모두 분으로 바꿔준 후 비율을 계산한다.
이 때 윤년인지 아닌지 판단하는 것이 중요하다.
코드
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include <iostream>
using namespace std;
bool isLeap(int year) {
if (year % 4 != 0) {
return false;
}
if (year % 100 != 0) {
return true;
}
if (year % 400 == 0) {
return true;
}
return false;
}
string months[] = { "","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int months_days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int main() {
string month, day, time;
int m, d, year, hour, min;
int days = 0;
double totaltime = 0;
double now = 0;
cin >> month >> day >> year >> time;
for (int i = 1; i <= 12; i++) {
if (month == months[i]) {
m = i;
}
}
d = ((int)day[0] - '0') * 10 + ((int)day[1] - '0');
hour = ((int)time[0] - '0') * 10 + ((int)time[1] - '0');
min = ((int)time[3] - '0') * 10 + ((int)time[4] - '0');
if (isLeap(year) == true) {
totaltime = 366 * 24 * 60;
for (int i = 0; i < m-1; i++) {
if (i == 1) days = days + isLeap(year);
days = days + months_days[i];
}
now = days * 24 * 60 + (d - 1) * 24 * 60 + hour * 60 + min;
cout << fixed;
cout.precision(16);
cout << (now / totaltime) * 100;
}
else {
totaltime = 365 * 24 * 60;
for (int i = 0; i < m - 1; i++) {
days = days + months_days[i];
}
now = days * 24 * 60 + (d - 1) * 24 * 60 + hour * 60 + min;
cout << fixed;
cout.precision(16);
cout << (now / totaltime) * 100;
}
}
|
cs |
Memo
이전에 string을 int로 변환하는 문제를 풀어봐서 조금 더 쉽게 접근하여 풀 수 있었다. 역시 많은 고민과 많은 문제를 풀어보는 경험은 가장 강력한 무기인 것 같다.