codeup 1928
问题 A: 日期差值
时间限制: 1 Sec 内存限制: 32 MB
提交: 2590 解决: 640
[提交][状态][讨论版][命题人:外部导入]
题目描述
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。
输入
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
输出
每组数据输出一行,即日期差值
样例输入
20130101
20130105
样例输出
5
#include<iostream>
using namespace std;
int main() {
int date1, date2;
while (cin >> date1 >> date2) {
int month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int year1, year2, month1, month2, day1, day2;
int sum = 0;
if (date2 <date1) {
int t = date1;
date1 =date2;
date2 = t;
}
year1 = date1 / 10000;
month1 =date1 / 100 % 100;
day1 = num1 % 100;
year2 = date2 / 10000;
month2 = num2 / 100 % 100;
day2 = date2 % 100;
for (int i = year1 + 1; i < year2; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
sum += 366;
}
else sum += 365;
}
if ((year1 % 4 == 0 && year1 % 100 != 0) || year1 % 400 == 0) {
month[2] = 29;
}
if (year1 == year2) {
for (int i = month1 + 1; i < month2; i++) {
sum += month[i];
}
if (month1 == month2) {
sum += day2 - day1;
}
else {
sum += month[month1] - day1;
sum += day2;
}
}
else {
for (int i = month1 + 1; i <= 12; i++) {
sum += month[i];
}
sum += month[month1] - day1;
if ((year2 % 4 == 0 && year2 % 100 != 0) || year2 % 400 == 0) {
month[2] = 29;
}
else month[2] = 28;
for (int i = 1; i < month2; i++) {
sum += month[i];
}
sum += day2;
}
cout << ++sum << endl;
}
return 0;
}
网友评论