2298 - 1-1-3 Friday the Thirteenth 黑色星期五
13号又是一个星期五。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400.csy神牛
这里有一些你要知道的:
1、1900年1月1日是星期一.
2、4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,平年2月有28天.
3、年份可以被4整除的为闰年(1992=4*498 所以 1992年是闰年,但是1990年不是闰年).
4、以上规则不适合于世纪年。可以被400整除的世纪年为闰年,否则为平年。所以,1700,1800,1900和2100年是平年,而2000年是闰年.
请不要调用现成的函数
请不要预先算好数据(就是叫不准打表)!
Input
(friday.in)
一个正整数n.
Output
(friday.out)
七个在一行且相分开的整数,它们代表13日是星期六,星期日,星期一...星期五的次数..
Examples
Input
20
Output
36 33 34 33 35 35 34
Solution C
#include "stdio.h" int main() { int s; int n; int a[7]={0},y[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,j; scanf("%d",&n); s=0; for(i=1900;i<=1900+n-1;i++) { for(j=1;j<=12;j++) { s+=13; a[s%7]++; s=s-13+y[j]; if(j==2&&(i%4==0&&i%100!=0||i%400==0)) s++; } } printf("%d %d ",a[6],a[0]); for(i=1;i<5;i++) printf("%d ",a[i]); printf("%d",a[5]); }
Solution C++
#include <iostream> using namespace std; int n; typedef struct date { int year,month,day; }date; int weekday(date a)//返回周几,0对应周日 { int tm=a.month>=3?(a.month-2):(a.month+10); int ty=a.month>=3?a.year:(a.year-1); return (ty+ty/4-ty/100+ty/400+(int)(2.6*tm-0.2)+a.day)%7; } void madd(date &q) { q.month++; if (q.month>12) { q.month-=12; q.year++; } } int main() { // freopen("friday.in","r",stdin); // freopen("friday.out","w",stdout); cin>>n; int i,a[9]={0},f,coun=0; date q; q.year=1900,q.month=1,q.day=13; while (coun++<n*12) { f=weekday(q); a[f]++; madd(q); } cout<<a[6]<<' '; for (i=0;i<5;i++) cout<<a[i]<<' '; cout<<a[i]; cout<<endl; // fclose(stdin); // fclose(stdout); return 0; }