牛客 KY258 : 日期累加
程序员文章站
2024-02-29 12:09:58
...
#include<iostream>
using namespace std;
int main(){
int m;
cin >> m;
int year, month, day, num;
static int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
for(int i = 0; i < m; ++i){
cin >> year >> month >> day >> num;
day += num;
while(day > arr[month]){
//天数大于当月最大天数, 需要进位
//每次循环都要重置2月的值, 因为年份可能改变
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
arr[2] = 29;
else
arr[2] = 28;
//月份进位
day -= arr[month];
month++;
//检查年份是否需要进位
if(month == 13){
year++;
month = 0;
}
}
//按格输出
printf("%4d-%02d-%02d\n", year, month, day);
}
return 0;
}