题目描述
描述
13号又是一个星期五。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400.
这里有一些你要知道的:
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年是闰年.
请不要调用现成的函数
请不要预先算好数据(就是叫不准打表)!
格式
PROGRAM NAME: friday
INPUT FORMAT:
(friday.in)
一个正整数n.
OUTPUT FORMAT:
(friday.out)
七个在一行且相分开的整数,它们代表13日是星期六,星期日,星期一...星期五的次数..
输入格式
20输出格式
36 33 34 33 35 35 34
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,k=6;
int day[10];
int month[15];
void fuzhi()
{
month[0]=31;
month[1]=31;
month[2]=28;
month[3]=31;
month[4]=30;
month[5]=31;
month[6]=30;
month[7]=31;
month[8]=31;
month[9]=30;
month[10]=31;
month[11]=30;
}
int main()
{
memset(day,0,sizeof(day));
//freopen("in.txt","r",stdin);
scanf("%d",&n);
day[6]++;
fuzhi();
for(int i=0;i<n;i++)
{
month[2]=28;
if ( ((1900+i)%4==0 &&(1900+i)%100!=0) ||(1900+i)%400==0)
{
month[2]=29;
}
for(int j=1;j<=12;j++)
{
if(i==0 && j==1) continue;
k=(k+month[j-1]%7)%7;
if(k==0) k=7;
day[k]++;
}
}
for(int i=6;i<=7;i++)
printf("%d ",day[i]);
for(int i=1;i<=5;i++)
printf("%d ",day[i]);
return 0;
}