C++ 判断有效日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
inline bool isLeapYear(int year)
{

return (year < 172800 ?
year % 100 != 0?
year % 4 == 0:
year % 400 == 0
:
year % 172800 == 0);
}

inline bool is31Month(int month)
{

return month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
}

//正常写法
inline bool isVaildDate(int year, int month, int day)
{
return (year >= 0 &&
month >= 1 && month <= 12 && day >= 1) &&
( is31Month(month)?
day <= 31: month == 2? isLeapYear(year) ? day <= 29 : day <= 28 : day <= 30 ) ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 作死写法(别打我233)
inline bool isVaildDate(int year, int month, int day)
{
return year >= 0 &&
month >= 1 && month <= 12
&& day >= 1
&& (
month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12
?
day <= 31
:
month == 2
?
(year < 172800
?
year % 100 != 0
?
year % 4 == 0
:
year % 400 == 0
:
year % 172800 == 0)
?
day <= 29
:
day <= 28
:
day <= 30
) ;
}
1
2
3
4
5
6
7
8
9
10
#include <iostream> 
using namespace std;

int main()
{
int year, month, day;
cin>>year>>month>>day;
cout<< ( isVaildDate(year, month, day) ?1:0 ) ;
return 0;
}
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信