本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1201
本题主要考察日期的计算, AC代码:
#include<stdio.h>
int isrunnian(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
else return 0;
}
int dijitian(int year,int month,int day)
{
int i,result=day;
for(i=1;i<month;i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
result+=31;
else if(i==4||i==6||i==9||i==11)
result+=30;
else if(i==2)
{
if(isrunnian(year))
result+=29;
else result+=28;
}
}
return result;
}
int main()
{
int year,month,day;
int i,n,j,cnt;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d-%d-%d",&year,&month,&day);
cnt=dijitian(year,month,day);
if(isrunnian(year))
cnt=366-cnt;
else cnt=365-cnt;
for(j=year+1;j<year+18;j++)
{
if(isrunnian(j))
cnt+=366;
else cnt+=365;
}
cnt+=dijitian(year+18,month,day);
if(month==2&&day==29)
printf("-1\n");
else printf("%d\n",cnt);
}
return 0;
}