【hihoCoder】1148:2月29日
问题:http://hihocoder.com/problemset/problem/1148
给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期)。
思路:
1. 将问题转换成求两个日期间有几个闰年
- 基本公式:闰年数(endYear,startYear) = 闰年数(0, endYear) - 闰年数(0, startYear)
- 注意点:区间的端点值
2. 闰年的要求
- 不能被100整除,能被4整除
- 或能被400整除
3. cnt = [i / 4]
- 对cnt = [i / 4]取整: 这个可以得出从0到i中有几个数是4的倍数 1*4, 2*4, 3*4, 4*4......
- 同理,可以求得有几个是100的倍数,几个是400的倍数
4. 关于两端,就是startYear和endYear是否应该再计算在内的问题
- 1到10之间有 10 -1 + 1 = 10 个数
- 所以应该注意区间的第一个数!直接减的话,会将它减掉了
5. 代码注意点
- 用scanf()来读入有一定格式的输入,string类型不用来存字符串的。
string str;
scanf("%s", str);//不应该这么用
char strs[];
scanf("%s", strs);
- scanf()需要包含头文件 #include <string.h>
代码:
#include <iostream>
#include <string.h>
using namespace std; bool isLeap(int year)
{
if ((year % != && year % == ) || (year % == && year % == ))
return true;
return false;
} int main()
{
int cnt, startDay, endDay, startYear, endYear, leapCnt, i = ;
char startMonth[], endMonth[]; cin >> cnt;
while (i++ < cnt)
{
leapCnt = ;
scanf("%s %d, %d", startMonth, &startDay, &startYear);
scanf("%s %d, %d", endMonth, &endDay, &endYear); //日期已经超过2月29日,则不应该再考虑startYear里面的2月29日
if ((strcmp(startMonth, "February") != && strcmp(startMonth, "January") != )
|| (strcmp(startMonth, "February") == && startDay > ))
startYear++;
//日期没有超过2月29日,则不应该再考虑endYear里面的2月29日
if (strcmp(endMonth, "January") ==
|| (strcmp(endMonth, "February") == && endDay < ))
endYear--;
leapCnt = (endYear / - endYear / + endYear / ) - (startYear / - startYear / + startYear / );
if (isLeap(startYear))//startYear里面的2月29日!
leapCnt++;
cout << "Case #" << i << ": " << leapCnt << endl;
}
return ;
}
【hihoCoder】1148:2月29日的更多相关文章
- hihoCoder 1148 2月29日
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年: ...
- hihocoder#1148 : 2月29日 计算闰年的个数
计算到某年为止的闰年数,其实很简单.设要计算的年为A,则到A年为止(含A年)的闰年数为: 闰年数=INT(A/)-INT(A/)+INT(A/) 这里:INT为取整数函数 #include <c ...
- Hihocoder 2月29日
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年 ...
- 2016年12月29日 星期四 --出埃及记 Exodus 21:24
2016年12月29日 星期四 --出埃及记 Exodus 21:24 eye for eye, tooth for tooth, hand for hand, foot for foot,以眼还眼, ...
- 2016年11月29日 星期二 --出埃及记 Exodus 20:20
2016年11月29日 星期二 --出埃及记 Exodus 20:20 Moses said to the people, "Do not be afraid. God has come t ...
- 2016年10月29日 星期六 --出埃及记 Exodus 19:14
2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...
- 2016年6月29日 星期三 --出埃及记 Exodus 14:26
2016年6月29日 星期三 --出埃及记 Exodus 14:26 Then the LORD said to Moses, "Stretch out your hand over the ...
- [MySQL]-->查询5天之内过生日的同事中的闰年2月29日问题的解决过程
前言: 上次写了查询5天之内过生日的同事中的跨年问题的解决过程,网址为:http://blog.csdn.net/mchdba/article/details/38952033 ,当中漏了一个闰年2月 ...
- SQL点滴6—“微软不认识闰年2月29日”&字符"N"的作用
原文:SQL点滴6-"微软不认识闰年2月29日"&字符"N"的作用 http://www.cnbeta.com/articles/50580.htm这个 ...
随机推荐
- 按钮button的css样式(扁平化底色)
.button { background-color: #ff0000; /* Green */ border: none; color: white; font-family:Arial; padd ...
- 文件描述符、文件表项指针、inode节点的关系
内核使用3种数据结构表示打开的文件,他们之间的关系决定了在文件共享方面一个进程对另一个进程的影响. (1) 每个进程在进程表中都有一个纪录项,纪录项中包含一张打开文件描述符表,每个文件描述符各占一项, ...
- 预处理指令 #import
vs class # import会包含这个类的所有信息,包括实体变量和方法, # @class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,先不考虑.
- CentOS下yum安装LAMP
1. 用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用/etc/init.d/httpd start ...
- ble示例代码
ble代码下载: https://github.com/sutogan4ik/Android-BLE-GATT-Master-Slave
- JQuery数组详解(含实例)
<!doctype html>jQuery数组处理详解(含实例演示)@Mr.Think 演示所用数组 var _mozi=['墨家','墨子','墨翟','兼爱非攻','尚同尚贤']; 1 ...
- Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备一)
上节中提到在遍历创建控件树后,执行了以下操作: 1. CDialogBuilder构建各控件对象并形成控件树,并返回第一个控件对象pRoot: 2. m_pm.AttachDialo ...
- Range Sum Query - Mutable
https://leetcode.com/problems/range-sum-query-mutable/ 因为数组会变动,所以缓存机制受到了挑战...每次更新数组意味着缓存失效,这样一更新一查找的 ...
- F#之旅5 - 小实践之下载网页(爬虫基础库)
参考文章:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/fvsc-download.html 参考的文章教了我们如 ...
- URL
URL的格式:protocol :// hostname[:port] / path / [;parameters][?query]#fragment URL出现了有+,空格,/,?,%,#,& ...