题目来源:http://116.56.140.75:8000/JudgeOnline/problem.php?id=1706
1706: 有这样的日期吗?
时间限制: 1 Sec 内存限制: 64 MB题目描述
有个人的历史没有学好而且喜欢糊弄人。当问到某个重大历史事件是何年何月何日时发生时,他会立刻毫不犹豫地信口就报出一个日期,人家以为他还真有学问,这么对答如流。其实,他说出的日期可能不合历法,压根就不存在的这样的日期。请编程判断某个给定的日期是否合乎历法。
输入
第一行有个整数n表示测试用例的个数(1≤n≤100)。其后的n行,每行有三个正整数y,m,d,表示一个日期的年月日。
输出
如果该日期是合乎历法的则输出yes,否则输出no;每个日期的输出单独占一行。
样例输入
5
2009 2 29
2000 2 29
1977 9 31
200 12 30
1800 12 32
样例输出
no
yes
no
yes
no
解析:主要在于闰年,大小月,二月的处理
代码:
#include<iostream>
using namespace std;
bool check(int year)
{
if (year % 100==0)
{
if (year % 400 == 0)
return 1;
else
return 0;
}
else
{
if (year % 4 == 0)
return 1;
else
return 0;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
int y, m, d;
cin >> y >> m >> d;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
if (d < 1 || d>31)
cout << "no" << endl;
else
cout << "yes" << endl;
}
else if (m == 4 || m == 6 || m == 9 || m == 11)
{
if (d < 1 || d>30)
cout << "no" << endl;
else
cout << "yes" << endl;
}
else
{
if (check(y) == true)
{
if (d < 1 || d>29)
cout << "no" << endl;
else
cout << "yes" << endl;
}
else
{
if (d < 1 || d>28)
cout << "no" << endl;
else
cout << "yes" << endl;
}
}
}
}
/**************************************************************
Problem: 1706
User: 201730685257
Language: C++
Result: 正确
Time:0 ms
Memory:1484 kb
****************************************************************/