题解 | #日期累加#

日期累加

https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d

#include<cstdio>
bool fun(int n){
	return n % 400 == 0 || n % 100 != 0 && n % 4 == 0;
}
int main(){
	int n;
	int mday[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	while (scanf("%d", &n) != EOF){
		for (int i = 0; i < n; i++){
			int year, month, day, gap;
			scanf("%d%d%d%d", &year, &month, &day, &gap);
			for (int i = 0; i < gap; i++){
				if (day < mday[month]){
					day++;
				}
				else if (fun(year) && month == 2 && day < mday[month]+1){
					day++;
				}
				else if (!fun(year) && day == mday[month] && month != 12){
					day = 1;
					month++;
				}
				else if (fun(year) && month == 2 && day == mday[month] + 1){
					day = 1;
					month++;
				}
				else if (fun(year) && month != 2 && day == mday[month]&&month!=12){
					day = 1;
					month++;
				}
				else {
					year++;
					month = 1;
					day = 1;
				}
			}
			printf("%04d-%02d-%02d\n", year, month, day);
		}
	}
}

全部评论

相关推荐

点赞 评论 收藏
分享
孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务