您的位置:首页 > 其它

USACO 1.1.3 Friday the Thirteenth

2012-02-20 19:17 375 查看
比较简单,直接模拟,需要注意闰年的判断和对不同月份日期的处理。

代码如下:

/*
ID: michael139
LANG: C
PROG: friday
*/
#include<stdio.h>
#include<string.h>
int is_leapYear(int y);
int main () {
FILE *fin  = fopen ("friday.in", "r");
FILE *fout = fopen ("friday.out", "w");
int n,i,j,cur,count[7];
while (fscanf(fin,"%d",&n) != EOF) {
memset(count,0,sizeof(count));
cur = 2;//Monday
for (i=1900;i<=1900+n-1;i++) {
for (j=1;j<=12;j++) {
cur += 12;
count[cur%7] ++ ;
if (j==1 || j==3 || j==5 || j==7 || j==8 || j==10 || j==12) {
cur += 19;
} else if (j==4 || j==6 || j==9 || j==11) {
cur += 18;
} else {
if (is_leapYear(i)) cur += 17;
else cur += 16;
}
}
}
for (i=0;i<7;i++) {
fprintf(fout,"%d",count[i]);
if (i!=6) fprintf(fout," ");
}
fprintf(fout,"\n");
}
return 0;
}
int is_leapYear(int y) {
return (y%4==0) && (y%100!=0 || y%400==0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: