-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy path2.8.cpp
More file actions
56 lines (50 loc) · 1.47 KB
/
2.8.cpp
File metadata and controls
56 lines (50 loc) · 1.47 KB
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
/*
* 题目名称:日期累加
* 题目来源:北京理工大学复试上机题
* 题目链接:http://t.cn/E9Yw0Cr
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
using namespace std;
int dayTable[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
bool IsLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int NumberOfYear(int year) { //返回该年天数
if (IsLeapYear(year)) {
return 366;
} else {
return 365;
}
}
int main() {
int year, month, day;
int number;
int caseNumber; //组数
scanf("%d", &caseNumber);
while (caseNumber--) {
scanf("%d%d%d%d", &year, &month, &day, &number);
int row = IsLeapYear(year);
for (int j = 0; j < month; ++j) {
number += dayTable[row][j];
}
number += day;
while (number > NumberOfYear(year)) { //确定年
number -= NumberOfYear(year);
year++;
}
month = 0;
row = IsLeapYear(year);
while (number > dayTable[row][month]) { //确定月
number -= dayTable[row][month];
month++;
}
day = number; //确定日
printf("%04d-%02d-%02d\n", year, month, day);
}
return 0;
}