Blue Bridge Cup Date Issue
This is the seventh question of the 2017 Blue Bridge Cup C Language Provincial Competition Group B
Title description
Xiao Ming is collating a batch of historical documents. Many dates appear in these historical documents.
Xiao Ming knows that these dates are from January 1, 1960 to December 31, 2059.
What troubles Xiaoming is that the format of these dates is very inconsistent, some use year/month/day, some use month/day/year, and some use day/month/year.
What is more troublesome is that the first two digits of the year are also omitted, so that there are many possible dates corresponding to a date in the literature.
For example, 02/03/04 may be March 04, 2002, February 03, 2004, or March 02, 2004.
Given a date in the literature, can you help Xiao Ming determine which possible dates correspond to it?
Enter
a date in the format "AA/BB/CC". (0 <= A, B, C <= 9)
Output
Output several different dates, one line for each date, the format is "yyyy-MM-dd".
Multiple dates are arranged from morning to night.
Sample input
02/03/04
Sample output
2002-03-04
2004-02-03
2004-03-02
Ideas:
- I just saw this question, but I thought it was very simple. . . However, I found that the situation is very complicated, and there are many things to be judged. As expected, it is also a later question, not too simple.
- My idea is that the main function receives data, and then after sorting the data in the main function, use a judge function to determine whether the date is legal, and output if it is legal.
- The judge function mainly judges whether the month is reasonable and whether the day is reasonable according to the month (first judge whether it is a 30/31-day month, if it is a February, then judge whether it is a leap year, and then check whether the date exceeds the limit).
- It should be noted that the same date is only output once , so in the main function we need to determine whether the parameters of the judge function are the same. If they are the same, it means the same date, and output it once.
AC code:
#include<bits/stdc++.h>
using namespace std;
void judge(int a,int b,int c)//判断是否合理 合理即输出
{
if(b<1||b>12)//月超标 pass
return;
if(c<1||c>31)//日超标 pass
return;
if((b==1||b==3||b==5||b==7||b==8||b==10||b==12)&&c>31)//如果是 1 3 5 7 8 10 12月 并且日期大于31 pass
return;
if((b==4||b==6||b==9||b==11)&&c>30)//如果是 4 6 9 11 月 并且日期大于30 pass
return;
if(b==2)//如果是2月就判断是不是闰年
{
if(a%400==0||(a%4==0&&a%100!=0))//是闰年
{
if(c>29)//大于29天 pass
return;
}
else
{
if(c>28)//大于28天 pass
return;
}
}
printf("%d-%02d-%02d\n",a,b,c);//成功输出
}
int main()
{
int a,b,c,a1,c1;
scanf("%d/%d/%d",&a,&b,&c);
a1=a;
c1=c;
if(a<=59)//加100方便比较
a1+=100;
if(c<=59)
c1+=100;
/*
三种情况
a b c
年 月 日 (1)
月 日 年 (2)
日 月 年 (3)
*/
if(a1<c1)//a小于c的话 先输出(1) 再(2)(3)
{
judge(1900+a1,b,c);//判断是否合理 合理即输出
int max_temp = max(a,b);//取b,c最大值和最小值
int min_temp = min(a,b);
judge(1900+c1,min_temp,max_temp);//按从小到大 输出(2)(3)
if(max_temp!=min_temp)//防止重复输出
judge(1900+c1,max_temp,min_temp);
}
else if(a1==c1)//只有一个年份
{
if(a==b)//三数相等就只输出一次
judge(1900+a1,b,c);
else//否则按顺序输出
{
int max_temp = max(a,b);//取b,c最大值和最小值
int min_temp = min(a,b);
judge(1900+a1,min_temp,max_temp);
judge(1900+c1,max_temp,min_temp);
}
}
else//先输出(2)(3)再(1)
{
int max_temp = max(a,b);//取b,c最大值和最小值
int min_temp = min(a,b);
judge(1900+c1,min_temp,max_temp);//按从小到大 输出(2)(3)
if(max_temp!=min_temp)//防止重复输出
judge(1900+c1,max_temp,min_temp);
judge(1900+a1,b,c);//判断是否合理 合理即输出 (1)
}
return 0;
}