码迷,mamicode.com
首页 > 其他好文 > 详细

题解报告:hdu1201(18岁生日)

时间:2018-02-24 16:47:07      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:clu   space   its   int   php   闰年   sum   pos   计算   

2018-02-24题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1201

解题思路:先判断一下如果出生这年是2月29号的话,则直接判断18岁那年是不是闰年,如果不是直接返回-1;

另外计算18岁内的日子总和,因为18岁那年的生日过完才算是真正的18岁,而此时已经经历了18年,所以先18*365赋给sum,接下来只需处理m和判断是否为闰年即可,是的话就加1

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int tol(int a,int b,int c);
 4 int main()
 5 {
 6     int T,y,m,d;
 7     while(cin>>T){
 8         for(int i=1;i<=T;i++){
 9             scanf("%d-%d-%d",&y,&m,&d);//注意格式
10             cout<<tol(y,m,d)<<endl;
11         }
12     }
13     return 0;
14 }
15 int tol(int a,int b,int c)
16 {
17     int sum=18*365;//先18*365再一次判断是否是闰年,是的话就加1
18     int h=(((a+18)%4==0)&&((a+18)%100!=0))||((a+18)%400==0);
19     if((b==2&&c==29)&&(!h))return -1;//18岁那年如果不是闰年的话,直接返回-1
20     int g=((a%4==0)&&(a%100!=0))||(a%400==0);
21     if(g&&(b<=2))sum+=1;
22     for(int i=a+1;i<=a+18;i++){
23         if(i!=a+18){
24             if(((i%4==0)&&(i%100!=0))||(i%400==0))sum+=1;
25         }
26         else{
27             if((b>2)&&h)sum+=1;
28         }
29     }
30     return sum;
31 }

 

题解报告:hdu1201(18岁生日)

标签:clu   space   its   int   php   闰年   sum   pos   计算   

原文地址:https://www.cnblogs.com/acgoto/p/8466037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!