hihoCoder2月29日(字符串模拟)
描述
给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期)。
只有闰年有2月29日,满足以下一个条件的年份为闰年:
1. 年份能被4整除但不能被100整除
2. 年份能被400整除
输入
第一行为一个整数T,表示数据组数。
之后每组数据包含两行。每一行格式为"month day, year",表示一个日期。month为{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}中的一个字符串。day与year为两个数字。
数据保证给定的日期合法且第一个日期早于或等于第二个日期。
输出
对于每组数据输出一行,形如"Case #X: Y"。X为数据组数,从1开始,Y为答案。
数据范围
1 ≤ T ≤ 550
小数据:
2000 ≤ year ≤ 3000
大数据:
2000 ≤ year ≤ 2×109
- 样例输入
-
4
January 12, 2012
March 19, 2012
August 12, 2899
August 12, 2901
August 12, 2000
August 12, 2005
February 29, 2004
February 29, 2012 - 样例输出
-
Case #1: 1
Case #2: 0
Case #3: 1
Case #4: 3 本题涉及到求两个时段之间闰年的公式#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
bool isLeap(int y)
{
if((y%==&&y%!=)||y%==)
return true;
return false;
}
struct Date{
char mon[];
int day;
long long year;
Date()
{
memset(mon,,sizeof(mon));
}
};
Date Beg;
Date End;
LL Leaps(LL y)
{
return y/-y/+y/;
}
int main()
{
int T;
scanf("%d",&T);
int cas=;
while(cas<=T)
{
scanf("%s",Beg.mon);
scanf("%d,",&Beg.day);
scanf("%lld",&Beg.year);
scanf("%s",End.mon);
scanf("%d,",&End.day);
scanf("%lld",&End.year);
LL res=;
if(isLeap(Beg.year))
{
if(!strcmp("January",Beg.mon)||!strcmp("February",Beg.mon))
res++;
}
if(isLeap(End.year))
{
if(!strcmp("February",End.mon)&&End.day==||(strcmp("January",End.mon)!=&&strcmp("February",End.mon)!=))
{
res++;
}
}
if(End.year==Beg.year&&isLeap(Beg.year)&&isLeap(End.year))
res--;
if(End.year->Beg.year)
res+=(Leaps(End.year-)-Leaps(Beg.year));
printf("Case #%d: %lld\n",cas++,res);
} return ;
}选一个基准日期,求两日期到基准日期距离的差。
#include <iostream>
#include <cstring>
using namespace std;
char month[][]={"Null","January","February","March","April","May","June","July", "August", "September", "October", "November","December"};
int d1,m1,y1;
int d2,m2,y2;
char mon1[],mon2[];
int sMon(char s[])
{
for(int i=;i<=;i++)
{
if(strcmp(month[i],s)==)
{
return i;
}
}
}
bool isLeap(int year)
{
if((year%==&&year%!=)||year%==)
{
return true;
}
return false;
}
int leaps(int year)
{
return year/-year/+year/;
}
int main()
{
int T;
cin>>T;
for(int cas=;cas<=T;cas++)
{
int lp1=;
int lp2=;
cin>>mon1>>d1;
cin.get();
cin>>y1;
cin>>mon2>>d2;
cin.get();
cin>>y2;
m1=sMon(mon1);
m2=sMon(mon2);
lp1+=leaps(y1-);
lp2+=leaps(y2-);
if(isLeap(y1))
{
if(m1>)
{
lp1++;
}
}
if(isLeap(y2))
{
if((m2==&&d2==)||m2>)
{
lp2++;
}
}
cout<<"Case #"<<cas<<": "<<lp2-lp1<<endl;
}
return ;
}
hihoCoder2月29日(字符串模拟)的更多相关文章
- 【hihoCoder】1148:2月29日
问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...
- SQL点滴6—“微软不认识闰年2月29日”&字符"N"的作用
原文:SQL点滴6-"微软不认识闰年2月29日"&字符"N"的作用 http://www.cnbeta.com/articles/50580.htm这个 ...
- Hihocoder 2月29日
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年 ...
- hihoCoder 1148 2月29日
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年: ...
- 数据可视化之DAX篇(一)Power BI时间智能函数如何处理2月29日的?
https://zhuanlan.zhihu.com/p/109964336 今年是闰年,有星友问我,在Power BI中,2月29日的上年同期是怎么计算的? 这是个好问题,正好梳理一下,Power ...
- 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 ...
随机推荐
- [Algorithom] Stack Data Structure in JavaScript
A stack is a collection of items that obeys the principle of "last in, first out". Like a ...
- 如何使用电骡eMule上传资源
1 在电脑中创建一个文件夹专门为上传资源使用 在里面放入任意文件并右击显示ED2K链接 链接效果如下 2 登陆verycd网站,并点击上传资源 复制ED2K地址并选择分类
- PHP计算两个时间差的方法
<?php //PHP计算两个时间差的方法 $startdate="2010-12-11 11:40:00"; $enddate="2012-12-12 11:45 ...
- Layout规则总结
一.尺寸要求 1.过孔到焊盘的距离多少合适? 6mil左右 2.铜皮到边框的距离多少合适? 极限8mil,通常12,最好做到20,40 3.Thermal焊盘打地孔个数? 正方形 3*3 4*4 ...
- C# 知识点随手学习网站推荐
http://www.studyofnet.com/news/list-8881.2-1-1.html
- 斯坦福公开课:Developing IOS 8 App with Swift(1-3)心得体会
最近开始学习Swift开发移动程序.跟随斯坦福大学的公开课进行自学. 这真是一个美好的时代,虽然不能在斯坦福求学,但是可以观看录制的授课录像.讲义,好似老师在给我们上课一样! 心得: 1.每节课信息量 ...
- Java泛型擦除
Java泛型擦除: 什么是泛型擦除? 首先了解一下什么是泛型?我个人的理解:因为集合中能够存储随意类型的对象.可是集合中最先存储的对象类型一旦确定后,就不能在存储其它类型的对象了,否则,编译时不会报错 ...
- nodejs-runoob
是否安装成功nodejs - node -v 是否安装成功npm - npm -v
- Nginx下的https配置
https: https(Secure Hypertext Transfer Protocol) 安全超文本传输协议 它是以安全为目标的http通道,即它是http的安全版.它使用安全套接字层(SSL ...
- Java多线程学习(吐血超详细总结)转自博主林炳文Evankaka
文章由林炳文Evankaka原创.转载自http://blog.csdn.net/evankaka 写在前面的话:此文只能说是Java多线程的一个入门,其实Java里头线程完全可以写一本书了,但是如果 ...