【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这个 ...
随机推荐
- C# 连接DB2字符串 Oracle免安装客户端连接字符串
以下是DB2连接数据库 1)使用IBM.Data.DB2链接DB2数据库 2)必须安装DB2客户端,IBM.Data.DB2在安装的BIN里可以找到 3)注意一下DB2客户端版本问题,我的就是WIN7 ...
- LeetCode 167 Two Sum II - Input array is sorted
Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...
- 手把手教你玩转nginx负载均衡(一)----使用vitualBox创建虚拟机
引言 作为一个web程序员,有时候需要想尽办法来利用有限的资源来产生最大程度的负载,除了提高硬件配置,增加带宽之外,CDN加速,DNS加速,缓存,还可以利用反向代理.但是要说反向代理,就不的不说ngi ...
- Windows Task Scheduler Fails With Error Code 2147943785
Problem: Windows Task Scheduler Fails With Error Code 2147943785 Solution: This is usually due to a ...
- Launching web on MyEclipse Tomcat 问题
错误提示: Launching web on MyEclipse Tomcat has encountered a problemAn internal error occurred during: ...
- [原创]Centos7 从零编译配置Memcached
序言 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. Memca ...
- Git - 问题集
1.If no other git process is currently running, this probably means a git process crashed in this re ...
- redhat6下安装Lighttpd1.4.43
学完了C语言,自信满满地冲着开源软件去了,首选了Lighttpd,这个软件代码量不多,适合初入开源的朋友 redhat下安装Lighttpd,一定要先安装依赖库,pcre和bzip2,这两个自行下载, ...
- Dos命令查看端口占用及关闭进程
1. 查看端口占用 在windows命令行窗口下执行: netstat -aon|findstr "8080" TCP 127.0.0.1:80 0.0.0.0:0 LISTENI ...
- shell获取文件最后100行,开头100行,指定开始行和结束行的内容
文件最后100行:tail -n100 filePath: 文件开头100行:head -n100 filePath: 文件指定开始行和结束行的内容:sed '1,100p' filePath: 文件 ...