题解 | 日期累加

#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int m;
	cin>>m;
	while(m){
		int Y,M,N,cal;
		int days=0;
		cin>>Y>>M>>N>>cal;
		bool leapyear=(Y%4==0&&Y%100!=0)||(Y%400==0);
		int month[]={31,leapyear?29:28,31,30,31,30,31,31,30,31,30,31};
		for(int i=0;i<M-1;i++){
			days+=month[i];
		}
		days=days+N+cal;
		while(days>=(leapyear?366:365)){
			days=days-(leapyear?366:365);
			Y++;
			leapyear=(Y%4==0&&Y%100!=0)||(Y%400==0);
		}
		leapyear=(Y%4==0&&Y%100!=0)||(Y%400==0);
		int remonth[]={31,leapyear?29:28,31,30,31,30,31,31,30,31,30,31};
		int i=0;
		int count=0;
		while(i<12){
			count=count+remonth[i];
			if(count>=days){
				M=i+1;
				N=days-(count-remonth[i]);
				break;
			}
			else i++;
		}
		printf("%04d-%02d-%02d\n",Y,M,N);
	    m--;	
	}
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务