题目
解释
- 遇到模拟查找日期合法性的问题
- 统一采用O(n)的遍历方式,这样也方便从小到大的输出
代码段
#include<iostream>
using namespace std;
int m[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int num[3];
char c[3][2];
bool check(int year, int month, int date)
{
if (month != 2)
{
if (month <= 12 && month >= 1 && date <= m[month] && date >= 1)
return true;
else
return false;
}
else
{
if (year % 4)
{
if (date <= 28 && date >= 1)
return true;
else
return false;
}
else
{
if (date <= 29 && date >= 1)
return true;
else
return false;
}
}
}
int main()
{
char t;
for (int i = 0; i < 3; i++)
{
cin.get(c[i], 3);
cin >> t;
for (int j = 0; j < 2; j++)
{
num[i] *= 10;
num[i] += c[i][j] - '0';
}
}
for (int i = 19600101; i <= 20591231; i++)
{
int year = i / 10000;
int month = i / 100 % 100;
int date = i % 100;
if (check(year, month, date))
{
if ((year % 100 == num[0] && month == num[1] && date == num[2])
|| (year % 100 == num[2] && month == num[1] && date == num[0])
|| (year % 100 == num[2] && month == num[0] && date == num[1]))
printf("%d-%02d-%02d\n", year, month, date);
}
}
}