迴圈
輸入年月日
分別計算共有幾天
輸出差值
#include <iostream>
#include <cmath>
using namespace std;
int calc(int year, int month, int date);
int main()
{
int year[2], month[2], date[2];
while(cin >> year[0] >> month[0] >> date[0])
{
cin >> year[1] >> month[1] >> date[1];
cout << abs(calc(year[0], month[0], date[0]) - calc(year[1], month[1], date[1]) ) << endl;
}
return 0;
}
int calc(int year, int month, int date)
{
int total;
if(year != 0)
total = (year - 1)*365;
else
total = 0;
for(int i=1; i < month; i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
total += 31;
else if(i==2)
total += 28;
else
total += 30;
}
total += date;
//閏年
total += year/4 - year/100 + year/400;
//今年
if ( ((year%4 == 0 && year%100 != 0) || year%400 == 0) && month <= 2)
total--;
return total;
}
留言列表