NYOJ 219

 

An problem about date

时间限制:2000 ms | 内存限制:65535 KB
难度:2
 
描述

acm的iphxer经常忘记某天是星期几,但是他记那天的具体日期,他希望你能写个程序帮帮他。

 
输入
每行有三个整数 year,month,day,日期在1600年1月1日到9600年1月1日之间;
输出
输出对应的星期,用一个整数表示;(星期一到星期六用1-6表示,星期日用0表示)
样例输入
2011 3 6
1949 10 1
2011 4 1
1945 8 15
样例输出
0
6
5
3
/*有人说蔡勒公式也可以,没试过*/
#include<stdio.h> #include<string.h> /*二月先按28天*/ int yue[13]={0,0,31,59,90,120,151,181,212,243,273,304,334}; /*不可开为8000,必须大于9602,否则因为非法内存而停止*/ int a[9603]; int is_leap(int year) { return year%400==0||year%4==0&&year%100; } int main() { int year,month,day,sum,i; memset(a,0,sizeof(a)); for(i=1600;i<9602;++i) if(is_leap(i)) a[i]=366+a[i-1]; else a[i]=365+a[i-1]; while(scanf("%d%d%d",&year,&month,&day)!=EOF) { sum=0; /*sum+=a[year-1],第year年之前 */ sum+=a[year-1];sum+=yue[month];sum+=day; if(is_leap(year)&&month>2) sum+=1; /*第一天星期6*/ printf("%d\n",(sum+5)%7); } return 0; }
原文地址:https://www.cnblogs.com/hxsyl/p/2553006.html