HDU 1201 18岁生日

18岁生日

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23160    Accepted Submission(s): 7404


Problem Description
Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他想请你帮忙计算一下他和他的几个朋友从出生到达18岁生日所经过的总天数,让他好来比较一下。
 
Input
一个数T,后面T行每行有一个日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。
 
Output
T行,每行一个数,表示此人从出生到18岁生日所经过的天数。如果这个人没有18岁生日,就输出-1。
 
Sample Input
1 1988-03-07
 
Sample Output
6574
 
Author
Gardon
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1205 1106 1215 1228 1234
 
这题就是需要注意一些细节,一个是开始的第一年是不是闰年,是闰年的话在2.29前面还是后面,一个是18岁那年是不是闰年,是的话在2.29前面还是后面,还有就是一个闰年的2.29的特判就没了。杭电讨论组里一个人的数据非常好,在代码最后的注释里。
 1 #include<math.h>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 #define N 155
 8 
 9 int y,m,d,day_cnt,f1;
10 
11 int judge(int year)
12 {
13     if( (year%4==0)&&(year%100!=0) || year%400==0 )return 1;
14     else return 0;
15 }
16 
17 int main()
18 {
19     int t;cin>>t;
20     while(t--)
21     {
22         day_cnt=f1=0;
23 
24         scanf("%d-%d-%d",&y,&m,&d);
25         if(judge(y)&&m==2&&d==29)
26         {
27             cout<<-1<<endl;
28             continue;
29         }
30         if(judge(y)&&m>=3){f1=-1;}
31         if(judge(y+18)&&m<3){f1=0;}
32         if(judge(y+18)&&m>=3){f1=1;}
33         for(int i=0;i<18;i++)
34         {
35             int yy=y+i;
36             if(judge(yy))day_cnt+=366;
37             else day_cnt+=365;
38         }
39         cout<<day_cnt+f1<<endl;
40     }
41     return 0;
42 
43 }
44 
45 /*
46 13
47 2004-1-22
48 2004-2-28
49 2004-2-29
50 2004-4-20
51 2003-2-20
52 2003-2-28
53 2003-3-20
54 2002-2-20
55 2002-2-28
56 2002-3-20
57 2001-2-20
58 2001-2-28
59 2001-3-20
60 */

原文地址:https://www.cnblogs.com/wmxl/p/4671721.html