[蓝桥杯2017初赛]日期问题 (思路)

在这里插入图片描述

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<sstream>
#define ll long long
#include<set>
using namespace std;
bool is(int x)    ///判断是否是闰年 
{
	if(x%400==0||x%4==0&&x%100!=0)
	{
		return 1; //是闰年 
	}
	return 0;
}
void i2s(int i,string &s)  ///将int  类型转换成string 类型 
{
	stringstream ss;
	ss<<i;
	ss>>s;
}
string f(int a,int b,int c)
{
	if(a>=0&&a<=59)
	{
		a+=2000;
	}
	else if(a>=60&&a<=99)
	{
		a+=1900;
	}
	else return " ";
	if(b<1||b>12)  return " ";
	if(c<1||c>31) return " ";
	
	switch(b)
	{
		case 2: if(is(a)&&c>29) return " ";
		        else if(!is(a)&&c>28) return " ";
		        break;
		case 4: if(c>30) return " ";
		          break;
		case 6:   if(c>30) return " ";    
		           break;
		           
	    case 9:   if(c>30) return " ";    
		           break;
		case 11:   if(c>30) return "";    
		           break;
        default: 
		           break;
	}
	string _a,_b,_c;
	i2s(a,_a);
	i2s(b,_b);
	i2s(c,_c);
	if(_b.length()==1) _b="0"+_b;
	if(_c.length()==1) _c="0"+_c;
	return _a+"-"+_b+"-"+_c;
}
int main()
{
	string in;
	cin>>in;
	int a=0;
	int b=0;
	int c=0;
	a=(in[0]-'0')*10+(in[1]-'0');
	b=(in[3]-'0')*10+(in[4]-'0');
	c=(in[6]-'0')*10+(in[7]-'0');
	string case1=f(a,b,c);
	string case2=f(c,a,b);
	string case3=f(c,b,a);
	
	set<string>ans;  ///去重 
	if(case1!=" ") ans.insert(case1);
	if(case2!=" ") ans.insert(case2);
	if(case3!=" ") ans.insert(case3);
	set<string>::iterator it;
	for(it=ans.begin();it!=ans.end();it++)
	{
		cout<<*it<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值