#13905: C++簡易參考解答


shawn2000100 (東華財金)

學校 : 國立東華大學
編號 : 57300
來源 : [27.53.168.5]
最後登入時間 :
2021-09-19 19:53:19
a263. 日期差幾天 -- 板橋高中練習題 | From: [36.227.221.182] | 發表日期 : 2018-05-13 18:15

#include <iostream>
using namespace std;

const int days[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool date_compare(int y1, int m1, int d1, int y2, int m2, int d2) {
if(y1 > y2)
return true;
else if(y1 == y2) {
if(m1 > m2)
return true;
else if(m1 == m2)
return (d1 > d2 ? true : false);
else if(m1 < m2)
return false;
} else if(y1 < y2)
return false;
}

int date_calculate(int y1, int m1, int d1, int y2, int m2, int d2) {
bool more_than_one = false;
if(y2 > y1)
more_than_one = true;

int count = -1;

if(more_than_one) {

for( ; y1 < y2; ++y1) {
for( ; m1 <= 12; ++m1) {
for( ; d1 <= days[m1]; ++d1)
++count;

if( 2 == m1 && ( (0 == y1 % 4 && 0 != y1 % 100) || (0 == y1 % 400)) )
++count;

d1 = 1;
}
m1 = 1;
}

for(m1 = 1; m1 < m2; ++m1) {
for(d1 = 1; d1 <= days[m1]; ++d1)
++count;

if( 2 == m1 && ( (0 == y1 % 4 && 0 != y1 % 100) || (0 == y1 % 400)) )
++count;

d1 = 1;
}

for( ; d1 <= d2; ++d1)
++count;

} else {

for( ; m1 < m2; ++m1) {
for( ; d1 <= days[m1]; ++d1)
++count;

if( 2 == m1 && ( (0 == y1 % 4 && 0 != y1 % 100) || (0 == y1 % 400)) )
++count;

d1 = 1;
}

for( ; d1 <= d2; ++d1)
++count;

}

return count;
}

int main() {

int year, month, day;
int year2, month2, day2;

while(cin >> year >> month >> day >> year2 >> month2 >> day2) {
if(date_compare(year, month, day, year2, month2, day2))
swap(year, year2), swap(month, month2), swap(day, day2);

cout << date_calculate(year, month, day, year2, month2, day2) << endl;
}

return 0;
}

 
ZeroJudge Forum