ACM乐园
Love Me,Love My Code!
posts - 53,  comments - 24,  trackbacks - 0
http://acm.hdu.edu.cn/showproblem.php?pid=1201

简单题,判断是否是闰年,如果18岁那一年是平年,而且他是2月29号生日的话,就不能过生日。将第0年和第18年合并为一年,如果他是2月29号前生日的话,就对第0年进行闰年判断,否则对第18年进行闰年判断。
以下是代码:
 1#include <iostream>
 2#include <algorithm>
 3#include <string>
 4
 5using namespace std;
 6
 7bool isleap(int year)
 8{
 9    if(year%400==0 || (year%100 && year%4==0))
10        return true;
11    return false;
12}

13
14int main()
15{
16    string str;
17    int year,day,month,i,result,t;
18    cin>>t;
19    while(t--)
20    {
21        cin>>str;
22        year=(str[0]-'0')*1000+(str[1]-'0')*100+(str[2]-'0')*10+str[3]-'0';
23        month=(str[5]-'0')*10+str[6]-'0';
24        day=(str[8]-'0')*10+str[9]-'0';
25        if(!isleap(year+18&& month==2 && day==29//不存在生日的条件
26        {
27            cout<<-1<<endl;
28            continue;
29        }

30        result=0;
31        for(i=1;i<=17;i++)                //对中间的17年进行计算
32            if(isleap(year+i))
33                result+=366;
34            else
35                result+=365;
36        if(month<=2 && day<=29)           //如果生日是2月29号前,就对第0年进行闰年判断
37            if(isleap(year))
38                result+=366;
39            else
40                result+=365;
41        else                              //否则对第18年进行闰年判断
42            if(isleap(year+18))
43                result+=366;
44            else
45                result+=365;
46        cout<<result<<endl;
47    }

48    return 0;
49}
posted on 2011-04-01 18:15 大大木马 阅读(569) 评论(2)  编辑 收藏 引用

FeedBack:
# re: HDU 1201
2012-11-14 23:27 | liaoke
这是错误的答案  回复  更多评论
  
# re: HDU 1201
2012-11-15 12:38 | 木马
AC了呀,哪里有问题?
@liaoke
  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(1)

随笔档案(53)

文章档案(2)

搜索

  •  

积分与排名

  • 积分 - 62416
  • 排名 - 352

最新评论

阅读排行榜

评论排行榜