Project Euler problem19

You are given the following information, but you may prefer to do some research for yourself.


1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?


以下是一些已知信息,但是或许你需要自己做一些其他的调查。

1900年1月1日是星期一。

30天的月份有:9月,4月,6月,11月。

此外的月份都是31天,当然2月除外。

2月在闰年有29天,其他时候有28天。

年份可以被4整除的时候是闰年,但是不能被400整除的世纪年(100的整数倍年)除外。

20世纪(1901年1月1日到2000年12月31日)一共有多少个星期日落在了当月的第一天?


#include<stdio.h>
#include<stdlib.h>


int isLeapYear(int year)
{
if(year % 400 == 0)
{
       return 1;
      }
     else if(year % 4 == 0 && year % 100 != 0)
     {
         return 1;
     } 
return 0;
}


int CountOfDays(int year, int month)
 {
     switch(month)
     {
         case 1:
         case 3:
         case 5:
         case 7:
         case 8:
         case 10:
         case 12:
             return 31;
 
         case 4:
         case 6:
         case 9:
         case 11:
             return 30;
             
         case 2: 
             if(isLeapYear(year))
             {
                 return 29;
             }
             return 28;
     }
     return 0;
 }


int countSunday(int year,int &weekOfYearBegin)
{
int week = weekOfYearBegin;
int month = 1;
int count=0;
for(;month<13;)
{
if((1+week)%7==0)
count++;
week = (week+(CountOfDays(year,month))%7)%7;
month++;
}
weekOfYearBegin = week;
return count;
}


int main()
{
int year = 1901;
int week = 1;
int sum = 0;


for(;year<2001;)
{
sum = sum+countSunday(year,week); 
year++;
}


printf("%d\n",sum);
system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值