全部博文(68)
分类: C/C++
2012-01-25 17:40:18
You are given the following information, but you may prefer to do some research for yourself.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
答案:171
#include
int main()
{
int d[1212]={0};
int year=1900, mon=1;
int i=0;
for(i=0; i<1212; i++)
{
year = i / 12 + 1900;
mon = i % 12 + 1;
if (i==0){
d[0]=1;
continue;
}
if ((year%4 != 0) || (year%100 == 0 && year % 400 != 0))
{
switch(mon)
{
case 1:
case 2:
case 4:
case 6:
case 8:
case 9:
case 11:
d[i] = 31+d[i-1];
break;
case 3:
d[i] = 28+d[i-1];
break;
case 5:
case 7:
case 10:
case 12:
d[i] = 30+d[i-1];
break;
}
}
else
{
switch(mon)
{
case 1:
case 2:
case 4:
case 6:
case 8:
case 9:
case 11:
d[i] = 31+d[i-1];
break;
case 3:
d[i] = 29+d[i-1];
break;
case 5:
case 7:
case 10:
case 12:
d[i] = 30+d[i-1];
break;
}
}
}
int count=0;
for (i=12; i<1212; i++)
if(d[i]%7 == 0) count++;
//printf("%5d ", d[i]);
printf("The number of Sunday is %d\n", count);
return 0;
}