题意
给出一个日期,算到十八岁时经过几天
思路
四年一闰,百年不闰,四百年再闰。
本年闰年:
* 在2/29日之前生日:到明年经过366天
* 在2/29日之后生日:到明年经过365天
本年平年:
* 来年闰年:
* * 在2/29日之前生日:到明年经过365天
* * 在2/29日之后生日:到明年经过366天
* 来年平年:到明年经过365天
2/29出生:18年后如果不是闰年,输出-1
代码
#include <iostream>
using namespace std;
int isrun(int year){
if((year%4==0&&year%100!=0)||(year%400==0)){
return 1;
}
else{
return 0;
}
}
int days(int year,int month,int day){
int s=0;
if(isrun(year)==1){
if((month==1)||(month==2&&day<29)){
s+=366;
}
else s+=365;
}
else{
if(isrun(year+1)==1){
if((month==1)||(month==2&&day<29)){
s+=365;
}else s+=366;
}else s+=365;
}
return s;
}
int main(int argc, char *argv[])
{
int n;cin>>n;
while(n--){
int year,month,day;
int i;
int sum=0;
scanf("%d-%d-%d",&year,&month,&day);
if((isrun(year+18)!=1)&&month==2&&day==29){
cout<<-1<<endl;
}else{
for(i=0;i<18;i++){
sum+=days(year,month,day);
year++;
}
cout<<sum<<endl;
}
}
return 0;
}