POJ 2080 Calendar 日期计算

就是一道简单的日期计算题

但是还是有很多要注意的问题,一不小心就会错,不过这种题目拿边缘数据测试就可以看出是哪里错了,比如30,365,36之类的..

#include <cstdio>
using namespace std;
int rm[]={31,29,31,30,31,30,31,31,30,31,30,31};
int pm[]={31,28,31,30,31,30,31,31,30,31,30,31};
int isr(int x){//判断闰年 
	if(x%400==0||x%100!=0&&x%4==0)return 1;
	return 0;
}
int main(){
	int n;
	while(scanf("%d",&n)){
		int y=2000,m=0,d=0,dd;
		if(n==-1)break;
		dd=n;
		while(n>=(isr(y)?366:365)){//减去年份,注意是>= 
			n-=(isr(y)?366:365);
			y++;
		}
		while(n>=(isr(y)?rm[m]:pm[m])){//减去月份 
			n-=(isr(y)?rm[m]:pm[m]);
			m=(m+1)%12;
		}
		d=n%(isr(y)?rm[m]:pm[m])+1;//算日期 
		printf("%d",y);
		m=m+1;
		m<10?printf("-0%d",m):printf("-%d",m);
		d<10?printf("-0%d ",d):printf("-%d ",d);
		switch(dd%7){
			case 1:printf("Sunday");break;
			case 2:printf("Monday");break;
			case 3:printf("Tuesday");break;
			case 4:printf("Wednesday");break;
			case 5:printf("Thursday");break;
			case 6:printf("Friday");break;
			case 0:printf("Saturday");break;
		}
		printf("\n");
		
	}
	return 0;
} 


你可能感兴趣的:(POJ 2080 Calendar 日期计算)