文章

56

粉丝

0

获赞

78

访问

12.8k

头像
日期累加 题解:C++ 注意:本题需要考虑闰年
P1446 北京理工大学机试题
发布于2025年9月1日 19:05
阅读数 83

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

struct data{
	int y;
	int m;
	int d;
};

// 判断年份是否是闰年
bool isRun(data a){
	if((a.y%4==0&&a.y%100!=0)||(a.y%400==0)){
		return true;
	}else{
		return false;
	}
} 

// 计算若干天之后的日期(需要考虑闰年) 
void sumHou(data a,int dsum){
	int num[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	if(isRun(a)){
		num[1]=29;
	}else{
		num[1]=28;
	}
	a.d+=dsum;
	while(a.d>num[a.m-1]){
		a.d-=num[a.m-1];
		a.m++;
		if(a.m>12){
			a.m-=12;
			a.y++;
		}
		if(isRun(a)){
			num[1]=29;
		}else{
			num[1]=28;
		}
	}
	if(a.m<10){
		if(a.d<10){
			cout<<a.y<<"-0"<<a.m<<"-0"<<a.d<<endl;
		}else{
			cout<<a.y<<"-0"<<a.m<<"-"<<a.d<<endl;
		}
	}else{
		if(a.d<10){
			cout<<a.y<<"-"<<a.m<<"-0"<<a.d<<endl;
		}else{
			cout<<a.y<<"-"<<a.m<<"-"<<a.d<<endl;
		}
	...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发