题解 | #日期累加#
日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d
#include <stdio.h> #define bool int #define true 1 #define false 0 int days(int flag,int month){//每月有天 int Day[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} }; return Day[flag][month]; } bool isRun(int year){//判断是否是闰年 if((year%4==0 & year%100!=0)||(year%400==0)){ return true; } return false; } int years(int year){ if(isRun(year)){ return 366; } return 365; } int main() { int c; int flag=0;//flag是1的时候代表是闰年 int year,month,day,num; scanf("%d",&c); while (c--) { // 注意 while 处理多个 case scanf("%d %d %d %d", &year,&month,&day,&num); flag=isRun(year); //计算年月日是一年的第几天 for(int i=0;i<month;++i){ num+=days(flag,i); } num+=day; //第几天加上 num while(num>years(year)){//确定年份 num-=years(year); year++; } flag=isRun(year);//年份可能更改后,重新确认是不是闰年 month=0; while(num>days(flag,month)){//确定月份 num-=days(flag,month); month++; } day=num;//确定天 printf("%04d-%02d-%02d\n", year,month,day); } return 0; }