Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendar's scheme of leap years as follows:
Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year.
In this problem, you have been given two dates and your task is to calculate how many days are between them. Note, that leap years have unusual number of days in February.
Look at the sample to understand what borders are included in the aswer.
The first two lines contain two dates, each date is in the format yyyy:mm:dd (1900 ≤ yyyy ≤ 2038 and yyyy:mm:dd is a legal date).
Print a single integer — the answer to the problem.
1900:01:01 2038:12:31
50768
1996:03:09 1991:11:12
1579
ps:感谢莫莫的解题报告
题目链接:http://codeforces.com/problemset/problem/304/B
绝对的水题,大家可以练练手,写写判断日期的函数。可能有的队WA了,应该是没考虑后面的日期小于前面的日期。
题意:
这个题意很简单,题目给定两个日期,求两个日期间隔的天数。
解题思路:
题目中说1900≤yy≤2038,我们可以先制定一个参考日期,1900.1.1,然后两个日期相对这个日期的天数,然后做差即可求出。读入两个字符串的格式固定,处理成日期也很简单。
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<algorithm> #include<map> using namespace std; int isrunyear(int yy) //判断是不是润年的函数 { if((yy%400==0)||(yy%4==0&&yy%100!=0)) return 1; else return 0; } int day[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}}; //二维数组来存储润年平年每个月份的天数 int calday(int yy,int mm,int dd) //计算当前日期与1900.1.1的相对距离天数 { int i; int sum=0; for(i=1900;i<yy;i++) { if(isrunyear(i)) sum+=366; else sum+=365; } int p=isrunyear(yy); for(i=1;i<mm;i++) sum+=day[p][i-1]; sum+=dd; return sum; } int main() { char a[11]; while(cin>>a) { int yy=(a[0]-'0')*1000+(a[1]-'0')*100+(a[2]-'0')*10+(a[3]-'0'); //字符串处理,处理年月日 int mm=(a[5]-'0')*10+(a[6]-'0'); int dd=(a[8]-'0')*10+(a[9]-'0'); int pa=calday(yy,mm,dd); //第一个日期的距离天数 cin>>a; yy=(a[0]-'0')*1000+(a[1]-'0')*100+(a[2]-'0')*10+(a[3]-'0'); mm=(a[5]-'0')*10+(a[6]-'0'); dd=(a[8]-'0')*10+(a[9]-'0'); int pb=calday(yy,mm,dd); //第二个日期的距离天数 int res=abs(pa-pb); //计算结果 cout<<res<<endl; } return 0; } /* 1900:01:01 2038:12:31 1996:03:09 1991:11:12 */