Training Contest 4 D - Dreamer

Bash just woke up from his sweetest dream ever. In his dream, he became the best Pokenom trainer — like no one ever was. It was on the date …

Unfortunately, Bash forgot the exact date. He only remembered that the date was written in format ‘DD MM YYYY’ with exactly 8 digits. He also remembers these 8 digits (but he does not remember their order). Of course, the date must be a valid date.

Since Bash was born on Jan 1st, 2000, he knows that the date was on or after Jan 1st, 2000. (Note that the date can be Jan 1st, 2000 — it means Bash is destined to be the best Pokenom trainer since he was born!).

Bash really wants to know the date when he become the best Pokenom trainer. How many possible valid dates could there be? What is the earliest valid date that Bash could become the best Pokenom trainer?

Notes
On a leap year, February has 29 days. Following are the rules for determining leap years:

A year divisible by 400 is a leap year,

A year divisible by 100 but not by 400 is NOT a leap year,

A year divisible by 4 but not by 100 is a leap year,

A year not divisible by 4 is NOT a leap year.

Input
The first line contains one integer t (1≤t≤50) — the number of test cases.

Each of the next t lines describes one test case, contains eight digits in the format ‘XX XX XXXX’ (eight digits, separated by two blank spaces). Note that the input might not represent a valid date.

Note that the first month of the year is represented by 01, and the first day of the month by 01.

Output
For each test case, output a single line containing the number of possible dates and the earliest date which Bash could become the best Pokenom trainer, in the format ‘DD MM YYYY’. If there are no valid dates, print a single line containing ‘0’ (zero) instead.

Sample Input 1

3
04 11 2018
23 45 6789
01 01 0002

Sample Output 1

524 18 11 2004
0
4 01 01 2000

啥也不说,直接看代码

#include<bits/stdc++.h>
using namespace std;
int ad,am,ay,d,m,y;
char s[20];
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int days(int y,int m)
{
	if(y%400==0||(y%100!=0&&y%4==0)){
		if(m==2)
			return 29;
	}
	return day[m];
}
bool judge()
{
	sscanf(s,"%2d",&d);
	sscanf(s+2,"%2d",&m);
	sscanf(s+4,"%4d",&y);
	if(m>=1&&m<=12&&y>=2000){
		if(d>=1&&d<=days(y,m)){
			return true;
		}
	}
	return false;
}
bool check()
{
	if(y<ay)
		return true;
	else 
		if(y==ay){
			if(m<am)
				return true;
			else 
				if(m==am){
					if(d<ad)
						return true;
				}
		}
	return false;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		ay=am=ad=10000;
		scanf("%s %s %s",s,s+2,s+4);
		sort(s,s+8);
		int cnt=0;
		do
		{
			if(judge()){
				cnt++;
				if(check()){
					ad=d;
					am=m;
					ay=y;
				}
			}
		}while(next_permutation(s,s+8));      //直接调用next_permutation函数进行全排列省去不少代码量
		if(cnt){
			printf("%d %02d %02d %d\n",cnt,ad,am,ay);
		}
		else{
			cout<<"0"<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值