您的位置:首页 > 其它

HDU 1201 18岁生日

2015-07-23 20:29 435 查看

18岁生日

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


[align=left]Problem Description[/align]
Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他想请你帮忙计算一下他和他的几个朋友从出生到达18岁生日所经过的总天数,让他好来比较一下。

[align=left]Input[/align]
一个数T,后面T行每行有一个日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。

[align=left]Output[/align]
T行,每行一个数,表示此人从出生到18岁生日所经过的天数。如果这个人没有18岁生日,就输出-1。

[align=left]Sample Input[/align]

1
1988-03-07

[align=left]Sample Output[/align]

6574

[align=left]Author[/align]
Gardon

[align=left]Source[/align]
Gardon-DYGG Contest 2

[align=left]Recommend[/align]
JGShining | We have carefully selected several similar problems for you: 1205 1106 1215 1228 1234

这题就是需要注意一些细节,一个是开始的第一年是不是闰年,是闰年的话在2.29前面还是后面,一个是18岁那年是不是闰年,是的话在2.29前面还是后面,还有就是一个闰年的2.29的特判就没了。杭电讨论组里一个人的数据非常好,在代码最后的注释里。

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 155

int y,m,d,day_cnt,f1;

int judge(int year)
{
if( (year%4==0)&&(year%100!=0) || year%400==0 )return 1;
else return 0;
}

int main()
{
int t;cin>>t;
while(t--)
{
day_cnt=f1=0;

scanf("%d-%d-%d",&y,&m,&d);
if(judge(y)&&m==2&&d==29)
{
cout<<-1<<endl;
continue;
}
if(judge(y)&&m>=3){f1=-1;}
if(judge(y+18)&&m<3){f1=0;}
if(judge(y+18)&&m>=3){f1=1;}
for(int i=0;i<18;i++)
{
int yy=y+i;
if(judge(yy))day_cnt+=366;
else day_cnt+=365;
}
cout<<day_cnt+f1<<endl;
}
return 0;

}

/*
13
2004-1-22
2004-2-28
2004-2-29
2004-4-20
2003-2-20
2003-2-28
2003-3-20
2002-2-20
2002-2-28
2002-3-20
2001-2-20
2001-2-28
2001-3-20
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: