USACO 1.1.3 Friday the Thirteenth
2009-08-26 14:02
363 查看
不管怎么说,自己对写总结之类的事情并不感冒,因为解出一道题会有一种由衷的成就感,但写总结就没有这种感觉,所以题做了十几道,但总结只写了两篇,今天突然想写总结,拖着总不是个事。
这道题的主要意思是:
西方人认为星期五和13不吉利,就像中国人认为猫头鹰和4不吉利一样。所以如果一个月的13号正好是星期五,那么这一天就太不吉利了,本题的是想让你写一个程序来判断从1900年开始的多少年内(最多四百年),每个月的13号是周一到周日的次数,通过这来破除封建迷信。
题目告诉你以下信息:
1) 1900年一月一日是周一。
2)每个月的天数。除二月的天数随是否是闰年变化外,别的月份天数都是恒定的。
3)告诉你闰年怎么判断,其实就是一个年份能被4但不能被100整除,或者能被400整除,那么这年就是闰年,否则就不是。
这题需要注意的一点就是:判断每月的第一天是周几,再此基础上来判断该月13号是周几。
下面是我的代码:
代码中本来有assert语句,但编译时会产生很多错误,所以就去掉了,感觉很纳闷,编译器有的是VC++6.0.
这道题的主要意思是:
西方人认为星期五和13不吉利,就像中国人认为猫头鹰和4不吉利一样。所以如果一个月的13号正好是星期五,那么这一天就太不吉利了,本题的是想让你写一个程序来判断从1900年开始的多少年内(最多四百年),每个月的13号是周一到周日的次数,通过这来破除封建迷信。
题目告诉你以下信息:
1) 1900年一月一日是周一。
2)每个月的天数。除二月的天数随是否是闰年变化外,别的月份天数都是恒定的。
3)告诉你闰年怎么判断,其实就是一个年份能被4但不能被100整除,或者能被400整除,那么这年就是闰年,否则就不是。
这题需要注意的一点就是:判断每月的第一天是周几,再此基础上来判断该月13号是周几。
下面是我的代码:
/* ID: LANG: C TASK: friday */ #include <stdio.h> #include <assert.h> enum week {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY}; //no use int isleap(int year) { if(((year%4 ==0)&&(year%100)!= 0)||(year%400 == 0)) return 1; else return 0; } int main () { int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; int count[7] = {0}; int firstday = 0;//每个月的第一天是周几,从1900.1.1开始 int i,j; FILE *fin = fopen("friday.in","r"); FILE *fout = fopen("friday.out","w"); int N;//记录输入的年数 fscanf(fin,"%d",&N); for(i = 0;i < N;++i) { for(j = 0;j < 12;++j) { int temp = (firstday+12)%7;//加12表示相对于第一天的位移 count[temp]++; if(j==1) //j==1时是二月 { firstday += (month[j]+isleap(1900+i)); } else { firstday += (month[j]); } firstday %= 7; } } fprintf(fout,"%d %d %d %d %d %d %d/n",count[5],count[6],count[0],count[1],count[2],count[3],count[4]); return 0; }
代码中本来有assert语句,但编译时会产生很多错误,所以就去掉了,感觉很纳闷,编译器有的是VC++6.0.
相关文章推荐
- USACO 1.1.3 Friday the Thirteenth
- USACO之Section1.1.3 Friday the Thirteenth
- USACO1.1.3--Friday the Thirteenth
- 【USACO题库】1.1.3 Friday the Thirteenth黑色星期五
- Usaco 1.1.3 黑色星期五(Friday the Thirteenth)
- USACO 1.1.3 Friday the Thirteenth
- USACO1.1.3 Friday the Thirteenth 13日星期五
- usaco1.1.3 Friday the Thirteenth黑色星期五
- USACO1.1.3[Friday The Thirteenth]
- USACO1.1.3 Friday the Thirteenth
- USACO 1.1.3 Friday the Thirteenth
- USACO 1.1.3 Friday the Thirteenth (friday)
- USACO1.1.3 Friday the Thirteenth (friday)
- USACO 1.1.3 - Friday the Thirteenth(模拟)
- USACO 1.1.3 Friday the Thirteenth
- USACO1.1.3 Friday the Thirteenth(黑色星期五)
- USACO 1.1.3 Friday the Thirteenth
- USACO1.1.3 Friday the Thirteenth (friday)
- [USACO 1.1.3] Friday the Thirteenth
- USACO1.1.3 Friday the Thirteenth