题目 数星期日 下列信息是已知的,当然你也不妨自己再验证一下。
1900年1月1日是星期一。 三十天在九月中, 四六十一也相同。 剩下都是三十一, 除去二月不统一。 二十八天平常年, 多加一天在闰年。 闰年指的是能够被4整除却不能被100整除的年份,或者能够被400整除的年份。 在二十世纪(1901年1月1日到2000年12月31日)中,有多少个月的1号是星期天? 代码
#include<iostream>
using namespace std;
int isrun(int n)
{
return n%4==0&&n%100!=0||n%400==0;
}
int main()
{
int week = 1,res = 0;
int x = 0;
int monday[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
for(int i = 1901; i <= 2000; i++)
{
if(isrun(i))
monday[1] = 29;
else
monday[1] = 28;
for(int j = 0; j < 12; j++)
{
int temp = (x+1)%7;
if((week+temp)==7)
{
res++;
cout << i<<" "<<j<<endl;
}
x+=monday[j];
}
}
cout << res<<endl;
}
最终答案 171