博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Project Euler Problem 19 Counting Sundays
阅读量:6212 次
发布时间:2019-06-21

本文共 1278 字,大约阅读时间需要 4 分钟。

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 
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;}

转载于:https://www.cnblogs.com/tigerisland/p/7564001.html

你可能感兴趣的文章
字符串连接比较(std::unique_ptr实现)
查看>>
性能产生的十大原因
查看>>
java对象引用,对象赋值
查看>>
Android 底层系统架构图
查看>>
应用程序开发者关于MeeGo平台的2010总结
查看>>
全方位掌握 NSIS 的使用[转]
查看>>
MySQL如何利用索引优化ORDER BY排序语句
查看>>
[NOI.AC]NOI2019省选模拟赛 第二场
查看>>
日均5亿查询量,京东到家订单中心ES架构演进
查看>>
Ahjesus获取自定义属性Attribute或属性的名称
查看>>
JVM虚拟机选项:Xms Xmx PermSize MaxPermSize区别(转)
查看>>
CCF 201403-2 窗口
查看>>
delete noprompt archivelog 报错ORA-00245,RMAN-08132
查看>>
tomcat添加context方式部署web应用
查看>>
docker监控
查看>>
uboot学习之一-----源码配置
查看>>
2012_7_5
查看>>
codeforces911G
查看>>
java 集合、接口
查看>>
ACM-ICPC(9/26)
查看>>