【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这个 ...
随机推荐
- Android导包导致java.lang.NoClassDefFoundError
摘要: SDK方法总数是不能超过65k的.是否也引入其他的三方库,导致总数超过限制.超出限制会导致部分class找不到,引发java.lang.NoClassDefFoundError.解决方法:近日 ...
- curses.h的安装和使用
gcc test.c -o test 用以上命令编译包含curses.h头文件的程序时会出现各种引用未定义的错误,并且已经安装了 kernel-devel ncurese-devel ncurese- ...
- 第二天--html+css
<!Doctype html><html> <head> <meta charset="utf-8"> ...
- Shell 字符串的截取
直接上代码了. linux-:/.sh #!/bin/sh STR=HelloWorld echo 'STR == ' $STR :} # == } #结果为World } # Use : ${STR ...
- C#可空类型
C#创建可空类型对于有些可选类型的时候特别好用.创建可空类型用法直接上图. 执行效果 用法 运行效果
- C# 操作excel单元格居中
C# 操作excel //导出Excel private void ExportExcel(string fileName, System.Data.DataTable myDGV, s ...
- 51nod1185(wythoff+高精度)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1185 题意:中文题诶- 思路:wythoff模板题,和51n ...
- 在Linux和Windows的Docker容器中运行ASP.NET Core
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 译者序:其实过去这周我都在研究这方面的内容,结果周末有事没有来得及总结为文章,Scott H ...
- Android网络定位服务定制简述
Android 添加高德或百度网络定位服务 Android的网络定位服务以第三方的APK方式提供服务,由于在国内Android原生自带的com.google.android.gms服务几乎处于不可用状 ...
- 清理SYSAUX表空间
1.查看SYSAUX表空间中数据分布情况 col SEGMENT_NAME for a30 set lines 999 select * from (select segment_name,PARTI ...