Sicily 1814. Date Calculation issues

Source: Internet
Author: User

"Turn from the network of the Great God, by myself to organize"

Description

Try a C + + class to represent a date, given 2 dates yyyy.mm.dd the number of days between two days.

Input

The 1th behavior is a positive integer t, which indicates the number of tests.

For each test point, the 1th and 2nd lines have two date yyyy.mm.dd respectively.

Output

For each test point, output a row of numbers that represents the difference in number of days.

Sample input Copy sample input to Clipboard
22000.02.282000.03.016297.01.212351.11.27
Sample Output
21440938

    • Leap year: A leap year that can be divisible by 4 and not divisible by 100 is a leap year, which can be divisible by 400
    • Calculate the start date is the day of the year Day1
    • Calculate the end date is the day of the year Day2
    • Calculates the number of days between two years, including the starting year, excluding the year of termination
    • Use the number of days in the previous step minus the day1 of the year in which the start date is, plus the day2 of the year in which the ending date is the answer.
Method one: "From @chenhq1991"

#include <iostream> #include <string>using namespace std;int daysofyear (int year) {if (year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0)) {return 366;//leap year}else{return 365;}} int daysofmonth (int year,int month) {switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 2:if (( Year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0)) return 29;elsereturn 28;case 4:case 6:case 9:case 11:return 30;default:return 0;}} Class Date{public:date (String Date) {year = month = Day = 0;int Firstdot = Date.find ('. ', 0);//The point at which the years are found int seconddot = Date. Find ('. ', Firstdot + 1);//found the point after the month//processing Dayint counter = 0;for (int i = Date.length ()-1; i > Seconddot; i--) {char d = da Te[i];int temp = d-48;for (int j = 0; J < counter; J + +) {temp = temp * 10;} Day = day + temp;counter++;} Counter = 0;//processing monthfor (int i = secondDot-1; i > Firstdot; i--) {char m = date[i];int temp = m-48;for (int j = 0; j < counter; J + +) {temp = temp * 10;} Month = month + temp;counter++;} COunter = 0;//processing yearfor (int i = firstDot-1; I >= 0; i--) {char y = date[i];int temp = y-48;for (int j = 0; J < Coun ter J + +) {temp = temp * 10;} Year = year + temp;counter++;}} int GetYear () {return year;} int GetMonth () {return month;} int GetDay () {return day;} Overload '-'//Ask the left time to be later than Right's friend int operator-(date left, date ') {int daysbeforeright = 0;//First calculates the day of the year for which the date is the for (i NT i = 1; I < right. GetMonth (); i++) {daysbeforeright = Daysbeforeright + daysofmonth (right. GetYear (), i);} Daysbeforeright = Daysbeforeright + right. GetDay ();//If 2000.1.1,daysbeforeright is 1int daysbeforeleft = 0;//calculates the date on the left is the day of the year for (int i = 1; i < Ieft. GetMonth (); i++) {daysbeforeleft = Daysbeforeleft + Daysofmonth (left). GetYear (), i);} Daysbeforeleft = Daysbeforeleft + left. GetDay (); int interval = 0;//Two date interval for (int i = right. GetYear (); I < left. GetYear (); i++) {interval = interval + daysofyear (i);} The actual interval interval = interval-daysbeforeright + Daysbeforeleft;return interval;} Private:int Year;int MontH;int Day;}; int main () {int casenum;cin >> casenum;while (casenum--) {string First, second;cin >> first >> second;d Ate one (first);D ate two (second),//Compare two time successively if (First.length () > Second.length ())//long time later {int interval = one-two; cout << interval << Endl;} Else{if (First.length () < Second.length ()) {int interval = two-one;cout << interval << Endl;} else//length equals {if (First > second)//string comparison size, large time later {int interval = one-two;cout << interval << Endl;} Else{int interval = two-one;cout << interval << Endl;}}} return 0;}

Method Two: "From @ Green Night"

#include <iostream> #include <string>using namespace std;int daysofyear (int year) {if (year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0)) {return 366;//leap year}else{return 365;}} int daysofmonth (int year,int month) {switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 2:if (( Year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0)) return 29;elsereturn 28;case 4:case 6:case 9:case 11:return 30;default:return 0;}} Class Date{public:date (String Date) {year = month = Day = 0;int Firstdot = Date.find ('. ', 0);//The point at which the years are found int seconddot = Date. Find ('. ', Firstdot + 1);//found the point after the month//processing Dayint counter = 0;for (int i = Date.length ()-1; i > Seconddot; i--) {char d = da Te[i];int temp = d-48;for (int j = 0; J < counter; J + +) {temp = temp * 10;} Day = day + temp;counter++;} Counter = 0;//processing monthfor (int i = secondDot-1; i > Firstdot; i--) {char m = date[i];int temp = m-48;for (int j = 0; j < counter; J + +) {temp = temp * 10;} Month = month + temp;counter++;} COunter = 0;//processing yearfor (int i = firstDot-1; I >= 0; i--) {char y = date[i];int temp = y-48;for (int j = 0; J < Coun ter J + +) {temp = temp * 10;} Year = year + temp;counter++;}} int GetYear () {return year;} int GetMonth () {return month;} int GetDay () {return day;} Overload '-'//Ask the left time to be later than Right's friend int operator-(date left, date ') {int daysbeforeright = 0;//First calculates the day of the year for which the date is the for (i NT i = 1; I < right. GetMonth (); i++) {daysbeforeright = Daysbeforeright + daysofmonth (right. GetYear (), i);} Daysbeforeright = Daysbeforeright + right. GetDay ();//If 2000.1.1,daysbeforeright is 1int daysbeforeleft = 0;//calculates the date on the left is the day of the year for (int i = 1; i < Ieft. GetMonth (); i++) {daysbeforeleft = Daysbeforeleft + Daysofmonth (left). GetYear (), i);} Daysbeforeleft = Daysbeforeleft + left. GetDay (); int interval = 0;//Two date interval for (int i = right. GetYear (); I < left. GetYear (); i++) {interval = interval + daysofyear (i);} The actual interval interval = interval-daysbeforeright + Daysbeforeleft;return interval;} Private:int Year;int MontH;int Day;}; int main () {int casenum;cin >> casenum;while (casenum--) {string First, second;cin >> first >> second;d Ate one (first);D ate two (second),//Compare two time successively if (First.length () > Second.length ())//long time later {int interval = one-two; cout << interval << Endl;} Else{if (First.length () < Second.length ()) {int interval = two-one;cout << interval << Endl;} else//length equals {if (First > second)//string comparison size, large time later {int interval = one-two;cout << interval << Endl;} Else{int interval = two-one;cout << interval << Endl;}}} return 0;}

Own, basically according to the idea of the great God

#include <iostream> #include <cstdio> #include <cctype> #include <iomanip> #include <vector > #include <cstring> #include <string> #include <fstream> #include <stack> #include < vector> #include <algorithm> #include <cmath>using namespace std; BOOL Runnian (int year) {if (year%400==0 | | (year%4==0 && year%100! = 0))    {return true; } else return false;} int year1,mouth1,day1,year2,mouth2,day2; int longmouth[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int shortmouth[12]={31,28,31,30,31,30,31,31,30,31,30,31};    Long days (int year,int Mouth,int day) {Long amounts = 0;    int i,j;    for (i=0;i<year;i++) {if (Runnian (i)) amounts++;    } amounts+=year*365;    if (Runnian (year)) {for (i=0;i<mouth-1;i++) amounts+=longmouth[i];    } else{for (i=0;i<mouth-1;i++) amounts+=shortmouth[i];    } Amounts+=day; return amounts;}    int main () {int t;    Long D; Char str[11];    cin>>t;        while (t--) {cin>>str;        year1= (str[0]-' 0 ') *1000+ (str[1]-' 0 ') *100+ (str[2]-' 0 ') *10+ (str[3]-' 0 ');        mouth1= (str[5]-' 0 ') *10+ (str[6]-' 0 ');        day1= (str[8]-' 0 ') *10+ (str[9]-' 0 ');        cin>>str;        year2= (str[0]-' 0 ') *1000+ (str[1]-' 0 ') *100+ (str[2]-' 0 ') *10+ (str[3]-' 0 ');        mouth2= (str[5]-' 0 ') *10+ (str[6]-' 0 ');        day2= (str[8]-' 0 ') *10+ (str[9]-' 0 ');        if ((D=days (year1,mouth1,day1)-days (year2,mouth2,day2)) >0) cout<<d<<endl;    else cout<<-d<<endl; } return 1;}


Sicily 1814. Date Calculation issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.