Project Euler 题解 #19 Counting Sundays
来源:互联网 发布:云计算怎么创业 编辑:程序博客网 时间:2024/06/06 02:09
题目:Counting Sundays
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.
解题思路
蔡勒公式:一种计算任何一日属一星期中哪一日的算法。
这个公式太牛B了,没看的很明白。
参考下这几个:
蔡勒公式的由来
求某一天是星期几
星期的计算
代码实现
//https://projecteuler.net/problem=19//Counting Sundays#include <cassert>#include <iostream>using namespace std;const char *DayInWeek[] = {"momday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};//返回值//0:星期一//...//6:星期天int DayOfWeek(int year, int month, int day){assert(year > 0);assert(1 <= month && month <= 12);assert(1 <= day && day <= 31);int dayofweek = 0;if (month == 1 || month == 2){month += 12;year--;}if ((year < 1752) || (year == 1752 && month < 9) || (year == 1752 && month == 9 && day < 3)){dayofweek = (day + 2*month + 3*(month + 1)/5 + year + year/4 + 5)%7;} else{dayofweek = (day + 2*month + 3*(month + 1)/5 + year + year/4 - year/100 + year/400)%7;}return dayofweek;}int _tmain(int argc, _TCHAR* argv[]){cout<<DayInWeek[DayOfWeek(2013, 9, 27)]<<endl;int count = 0;for (int year = 1901; year <= 2000; ++year){for (int month = 1; month <= 12; ++month){if (6 == DayOfWeek(year, month, 1)){++count;}}}cout<<"There are "<<count<<" Sundays fell on the first of the month during the twentieth century"<<endl;system("pause");return 0;}
输出:171
有位读者提出公式1200/7 = 171
- Project Euler 题解 #19 Counting Sundays
- 19 Counting Sundays - Project Euler
- Project Euler 19: Counting Sundays
- project euler 19 Counting Sundays
- Project Euler:Problem 19 Counting Sundays
- Project Euler Problem 19 Counting Sundays
- Euler: Counting Sundays
- Problem 19:Counting Sundays
- PE 19 Counting Sundays
- Counting Sundays
- projecteuler---->problem=19----Counting Sundays
- Problem 19 Counting Sundays (计算星期天)
- [projecteuler]Counting Sundays
- Project Euler:Problem 72 Counting fractions
- Project Euler:Problem 76 Counting summations
- Project Euler:Problem 85 Counting rectangles
- 欧拉项目 第19题 Counting Sundays
- Project Euler 题解 #30 Digit fifth powers
- MySQL使用总结
- Ubuntu Server设置IP地址、网关、DNS
- sublime text 使用
- 设计模式学习总结
- poj 1830 开关问题(高斯消元)
- Project Euler 题解 #19 Counting Sundays
- iOS7(Xcode5)中隐藏状态栏的方法
- C# - Capture Full IE Document
- 数据字典表col$用于查看表都有什么列名
- linux c读取配置文件打印多出输出最后一行问题
- 遍历目录树中所有模式匹配的文件
- 使用 SQL Developer 将 SQL Server 数据库移植到 Oracle 上
- 【iOS开发】---- iOS7适配问题
- Android调用天气预报的WebService简单例子