Hangzhou Electric HDU 1201 18-year-old birthday

Source: Internet
Author: User

18-year-old birthday Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 21849 Accepted Submission (s): 6922


Problem Descriptiongardon's 18 birthday is coming, he is certainly very happy, but he suddenly thought of a question, is everyone from birth, to reach the 18 birthday when the number of days are the same? It doesn't seem to be all that, so he wants to ask you to calculate the total number of days he and his friends have passed since they were born to their 18 birthday.

Input is a number T, and the following T line has a date on each line, and the format is YYYY-MM-DD. As my birthday is in 1988-03-07.

Outputt lines, one number per line, indicating the number of days that the person has passed from birth to the age of 18. If this person does not have a 18 birthday, then output-1.

Sample Input
11988-03-07

Sample Output
6574

Authorgardon
Sourcegardon-dygg Contest 2 mama's eggs are getting a little bit of a problem. By actually trying to figure out the algorithm before you do it, it will save a lot of time, and then reconsider the optimization particularly much. Code One:
#include <iostream>using namespace Std;int main () {int T; Cin>>t;int year, Mouth,day;char Ch1,ch2;while (t--) {int Sum=0;int flag=0;cin>>year>>ch1>> mouth>>ch2>>day;for (int k=year;k<=year+18;k++) {if (k%4==0&&k%100!=0) | | (k%400) ==0) {if (mouth==2&&day==29) {cout<<-1<<endl;flag=1;break;} if (k==year) {if (mouth==2) sum+=29-day;else if (mouth==1| | mouth==3| | mouth==5| | mouth==7| | mouth==8| |mouth==10| | mouth==12) sum+=31-day;else sum+=30-day;for (int j=mouth+1;j<=12;j++) {if (j==1| | j==3| | j==7| | j==8| |j==10| | j==12| | j==5) Sum+=31;else if (j==2) sum+=29;elsesum+=30;}} else if (k==year+18) {for (int r=1;r<mouth;r++) {if (r==1| | r==3| | r==7| | r==8| |r==10| | r==12| | r==5) Sum+=31;else if (r==2) sum+=29;elsesum+=30;} Sum+=day;} else sum+=366;} Else{if (k==year) {if (mouth==2) sum+=28-day;else if (mouth==1| | mouth==3| | mouth==5| | mouth==7| | mouth==8| |mouth==10| | MOUTH==12) sum+=31-day;elsesum+=30-day;for (int j=mouth+1;j<=12;j++) {if (j==1| | j==3| | j==7| | j==8| |j==10| | j==12| |j==5) Sum+=31;else if (j==2) sum+=28;elsesum+=30;}} else if (k==year+18) {for (int t=1;t<mouth;t++) {if (t==1| | t==3| | t==7| | t==8| |t==10| | t==12| | t==5) Sum+=31;else if (t==2) sum+=28;elsesum+=30;} Sum+=day;} elsesum+=365;}} if (flag!=1) Cout<<sum<<endl;} return 0;}
Code two:
#include <iostream>using namespace Std;bool leapyear (int n)    //To determine if a leap year {if (n%4==0&&n%100!=0| | n%400==0) return 1;else return 0;} int peryear (int n)//Determine and return the number of days in a year {return leapyear (n)? 366:365;} int main () {int T;    Cin>>t;int year, Mouth,day;char Ch1,ch2;while (t--) {int sum=0;cin>>year>>ch1>>mouth>> Ch2>>day;if (mouth==2&&day==29)//As long as February appears 29th number must be leap year so 18 years later without her birthday {cout<<-1<<endl;continue;} for (int k=year;k<year+18;k++)//start and date of the month in the end of the year simultaneously delete {sum+=peryear (k);} if (MOUTH>2)//Imagine if the mouth is "2 when deleted, and the beginning and end of a year are leap years events occur {//Then inevitably there is a deletion when February days are different then fix the sum and in this case output       if (Leapyear ( year)) sum--;    else if (Leapyear (year+18)) Sum++;cout<<sum<<endl;} else Cout<<sum<<endl;}}

Hangzhou Electric HDU 1201 18-year-old birthday

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.