题解 | #日期累加#

日期累加

https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40&tqId=31013&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan&difficulty=&judgeStatus=&tags=/question-ranking

通过暴力计算,遍历每个月的天数即可。(tip:day在开始时设为0,天数加到plus上,便于处理。)

#include<bits/stdc++.h>
using namespace std;

int monthDay[2][12] = {
		{31,28,31,30,31,30,31,31,30,31,30,31},// 非闰年 
		{31,29,31,30,31,30,31,31,30,31,30,31}};// 闰年 
int isRunYear(int year) {
	return (year%4==0&&year%100!=0) || (year%400==0);
}

int main() {
	int year, month, day, plusDay;
	int n;
	while(scanf("%d", &n) != EOF) {
		for(int i=0; i<n; i++) {
			// 1992 11 22 316
			scanf("%d%d%d%d", &year, &month, &day, &plusDay);
			int runOrPing = isRunYear(year);
			plusDay += day;
			day = 0;
			while(plusDay>monthDay[runOrPing][month-1]) {
				plusDay-=monthDay[runOrPing][month-1];
				if(month == 12) {
					year++;
					month = 1;
					runOrPing = isRunYear(year);
				} else {
					month++;
				}
			}
			day = plusDay;
			printf("%d-%02d-%02d\n", year, month, day);
		} 
	}
	return 0;
}

全部评论

相关推荐

挣K存W养DOG:入职送金条全球游,路过缅甸停一下🐔
点赞 评论 收藏
分享
10-17 17:14
门头沟学院 C++
牛客410039819号:北京地区大多是919和927,这两场挂太多人了
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务