Last active
November 5, 2019 08:48
-
-
Save pinglunliao/31b78d96796d21fb2354 to your computer and use it in GitHub Desktop.
a263: 日期差幾天 FYI: https://yunlinsong.blogspot.com/2015/10/blog-post.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
using namespace std; | |
int daysPerMon[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
bool isLeapYear(int year) | |
{ | |
return (year % 4 == 0 && year % 100 || year % 400 == 0); | |
} | |
int daysCnt(int year, int month, int day) | |
{ | |
int sumOfDays = 0; | |
for(int i = 1; i < year; i++) | |
{ | |
bool isLeap = isLeapYear(i); | |
if(isLeap) | |
{ | |
sumOfDays += 366; | |
} | |
else | |
{ | |
sumOfDays += 365; | |
} | |
} | |
for(int i = 1; i < month; i++) | |
{ | |
sumOfDays += daysPerMon[i]; | |
} | |
if(isLeapYear(year) && month > 2) | |
sumOfDays++; | |
sumOfDays += day; | |
//cout << "Total Days:" << sumOfDays << endl; | |
return sumOfDays; | |
} | |
int dateDif(int y1, int m1, int d1, int y2, int m2, int d2) | |
{ | |
return daysCnt(y1, m1, d1) - daysCnt(y2, m2, d2); | |
} | |
int main() { | |
int year[2], month[2], day[2]; | |
while(scanf("%d%d%d %d%d%d", &year[0], &month[0], &day[0], &year[1], &month[1], &day[1]) != EOF) | |
{ | |
int result = dateDif(year[0], month[0], day[0], year[1], month[1], day[1]); | |
if( result < 0) | |
result = - result; | |
cout << result << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment