(HDU)1201 -- 18岁生日

题目链接:http://vjudge.net/problem/HDU-1201

分析:这个题目要注意的是输出-1是指这个人的生日在某个闰年的2月29日,那他只能过4的倍数的生日。

  还有一点要注意他的生日日期是在2月及以前还是3月及以后,这个和计算的起始年份有关。

  如果他的生日在1、2月,那么就以当前年为起始年份,如果今年是闰年就要加一了;

  如果他的生日在3月及以后,当前年是不是闰年没有影响,下一年作为起始年份考虑。

  然后循环分析18年是不是闰年...

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <string>
 7 #include <cstdlib>
 8 
 9 using namespace std;
10 
11 
12 int main()
13 {
14     int t,y,m,d,i;
15     scanf("%d",&t);
16     while(t--)
17     {
18         int ans=0,flag=1;
19         scanf("%d-%d-%d",&y,&m,&d);
20         if(m==2&&d==29) flag=0;
21         else
22         {
23             if(m>2)
24             {
25                 for(i=y+1;i<=y+18;i++)
26                 {
27                 if((i%4==0&&i%100!=0) || i%400==0)
28                     ans+=1;
29                     ans+=365;
30                 }
31             }
32         else
33             {
34                 for(i=y;i<=y+17;i++)
35                 {
36                 if((i%4==0&&i%100!=0) || i%400==0)
37                     ans+=1;
38                     ans+=365;
39                 }
40             }
41         }
42         if(flag) printf("%d
",ans);
43         else printf("-1
");
44     }
45     return 0;
46 }
原文地址:https://www.cnblogs.com/ACDoge/p/6133186.html