强化阶段 Day 4 算法笔记 3.4 日期处理

1.日期处理

用一个month数组和isleap函数解决闰年和平年的麻烦

#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;

int month[13][2] ={
   
   {0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};
bool isleap(int year){
	return (year%4==0&&year%100==0)||(year%400==0);
}

int main(){
	
	int time1,y1,m1,s1;
	int time2,y2,m2,s2;
	while(scanf("%d %d",&time1,&time2)!=EOF){
		if(time1>time2){
			int temp=time1;
			time1 = time2;
			time2 = temp;
		}
		y1= time1/10000;
		m1= time1%10000/100;
		s1= time1%100;
		y2= time2/10000;
		m2= time2%10000/100;
		s2= time2%100;
		
		int ans = 1;
		while(y1<y2||m1<m2||s1<s2){
			s1++;
			if(s1==month[m1][isleap(y1)]+1){
				m1++;
				s1=1;
			}
			if(m1==13){
				y1++;
				m1=1;
			}
			ans++;
		}
		printf("%d\n",ans);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aixiaoxiao13/article/details/121749151