日期差值
样例输入
20130101
20130105
样例输出
5
#include<stdio.h> #include<iostream> using namespace std; int main() { int month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; long int i = 0,sum1,sum2; int all1,all2,year1, year2, month1, month2, day1, day2; cin >> all1 >> all2;//获取两个出生日期 year1 = all1 / 10000;//得到出生年份 year2 = all2 / 10000; month1 = (all1 - year1 * 10000) / 100;//出生的月份 month2 = (all2 - year2 * 10000) / 100; day1 = all1 - year1 * 10000 - month1 * 100;//出生的日期 day2 = all2 - year2 * 10000 - month2 * 100; sum1 = (year1 - 1) / 4 + (year1 - 1) * 365;//年份是从公元01年开始的 for (; i < month1-1; i++) { if (year1 % 2 == 0 && i == 1) sum1 += 29;//如果该年为闰年,2月有29天 else sum1 += month[i]; } sum1 += day1;//把日期加上,大功告成! sum2 = (year2 - 1) / 4 + (year2 - 1) * 365; for (i=0; i < month2 - 1; i++) { if (year2 % 2 == 0 && i == 1) sum2 += 29; else sum2 += month[i]; } sum2 += day2; if (sum1 > sum2) cout << sum1 - sum2;//输出两个人出生日期间差的天数。 else cout << sum2 - sum1; }