Project Euler Problem 19 Counting Sundays
2017-03-27 04:01
357 查看
Counting Sundays
Problem 19
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)?
C++:
#include <iostream>
using namespace std;
const int DAYS = 7;
enum DAY {SUN=0, MON, TUE, WED, THU, FRI, SAT};
int days[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 闰年计算函数
int leapyear(int year) {
if((year%4 == 0 && year%100 != 0) || year%400 == 0)
return 1;
return 0;
}
int main()
{
int sum=0, startday = MON;
for(int i=1900; i<=2000; i++) {
days[1] = leapyear(i) ? 29 : 28;
for(int j=0; j<12; j++) {
startday += days[j];
startday %= DAYS;
if(startday == SUN) // first is SUN day
if(i >= 1901)
sum++;
}
}
if(startday == SUN)
sum--;
cout << sum << endl;
return 0;
}
Problem 19
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)?
C++:
#include <iostream>
using namespace std;
const int DAYS = 7;
enum DAY {SUN=0, MON, TUE, WED, THU, FRI, SAT};
int days[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 闰年计算函数
int leapyear(int year) {
if((year%4 == 0 && year%100 != 0) || year%400 == 0)
return 1;
return 0;
}
int main()
{
int sum=0, startday = MON;
for(int i=1900; i<=2000; i++) {
days[1] = leapyear(i) ? 29 : 28;
for(int j=0; j<12; j++) {
startday += days[j];
startday %= DAYS;
if(startday == SUN) // first is SUN day
if(i >= 1901)
sum++;
}
}
if(startday == SUN)
sum--;
cout << sum << endl;
return 0;
}
相关文章推荐
- http://projecteuler.net/problem=19 [Answer:171]
- Project Euler Problem 19
- project euler problem 19 数周末
- Project Euler problem 19
- project euler Problem 15
- Project Euler Problem 387 - Harshad Numbers - 深度优先
- http://projecteuler.net/problem=38 [Answer:932718654]
- Project Euler Problem 75
- Project Euler Problem 543 Prime-Sum Numbers
- projecteuler Problem 20
- project euler problem 13:Large sum
- project euler problem 18 动态规划
- project euler problem 24
- Project Euler problem 41
- Project Euler problem 69
- http://projecteuler.net/problem=20 [Answer:648]
- http://projecteuler.net/problem=35 [Answer:55]
- Project Euler Problem 1: Multiples of 3 and 5
- Project Euler Problem 9: Special Pythagorean triplet
- Project Euler Problem 47 Distinct primes factors