HDOJ 1201 18岁生日 [常识]

 题目很水,但是我做得很纠结。

 

解题思路:
生活常识,平年:365天,闰年:366(2月有29号)
如果出生在2月29号,那么也就说出生那一年一定是闰年,通过简单证明可得:闰年+18=非闰年

 

提示:对大于等于3月和小与3月出生分类讨论。

 

AC代码:

 

#include <iostream> using namespace std; bool rn(int year) { if(year%4==0&&year%100!=0||year%400==0) return true; else return false; } int main() { int y,m,d,n,k; char c; cin>>n; while(n--) { k=0; cin >> y >> c >> m >> c >> d; if(m==2&&d==29) { cout<<"-1"<<endl; continue; } else{ if(m>=3) { for(int i=y+1;i<y+19;++i) if(rn(i)) k++; } else { for(int i=y;i<y+18;++i) if(rn(i)) k++; } } cout<<(18*365+k)<<endl; } return 0; }

 

 

测试数据:

 

10
1980-02-29
-1
1988-03-03
6574
1987-03-03
6575
1970-03-03
6575
1988-02-02
6575
1987-02-02
6575
1970-02-02
6574
1988-02-28
6575
1970-02-28
6574
1987-02-28
6575
Press any key to continue . . .

你可能感兴趣的:(c,生活,测试)