2017第八届蓝桥杯省赛C++B组【第七题:日期问题】

第七题

标题:日期问题

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。

比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。

给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

输入

---- 一个日期,格式是"AA/BB/CC"(0 <= A, B, C <= 9)

输入

 输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。

样例输入

02/03/04

样例输出

2002-03-04 
2004-02-03 
2004-03-02

资源约定: 峰值内存消耗(含虚拟机) < 256M CPU消耗 < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

注意: main函数需要返回0; 只使用ANSI C/ANSI C++ 标准; 不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include 不能通过工程设置而省略常用头文件。

提交程序时,注意选择所期望的语言类型和编译器类型。

代码

#include <iostream>
#include <algorithm>
using namespace std;
const int year1 = 1900;
const int year2 = 2000;
int mo[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
struct node{
	int year,month,day;
	bool operator == (const node& d)
      {
      	if(year==d.year && month==d.month && day==d.day) return true;
      	else return false;
      }
} data[10];
int cnt,a,b,c;
string str;

bool cmp(node n1,node n2){
	if(n1.year == n2.year){
		if(n1.month == n2.month){
			return n1.day < n2.day;
		}
		else return n1.month < n2.month;
	}
	else return n1.year < n2.year;
}

void judgeSave(int y,int m,int d){//判断并保存 
	if(y < 1960 || y > 2059) return;//年份
	
	if(m < 1 || m > 12) return;//月份
	 
	//平年 2月不超过29 
	if(y%4!=0 || (y%4==0&&y%100==0&&y%400!=0)){
		if(m==2 && d >= 29) return;    
	} 
	//对应月份的天数不对 
	if(d > mo[m] ||d < 1) return;
	
	//save
	data[cnt].year = y,data[cnt].month = m,data[cnt++].day = d;	
}

void solve(){
	cin >> str;
	for(int i = 0;i < 2;i++) a = a*10+str[i]-'0';
	for(int i = 3;i < 5;i++) b = b*10+str[i]-'0';
	for(int i = 6;i < 8;i++) c = c*10+str[i]-'0'; 
	judgeSave(a+year1,b,c);
	judgeSave(c+year1,b,a);
	judgeSave(c+year1,a,b);
	judgeSave(a+year2,b,c);
	judgeSave(c+year2,b,a);
	judgeSave(c+year2,a,b);
	
	sort(data,data+cnt,cmp);//排序
	int fcnt = unique(data,data+cnt)-data;//去重
//	cout << fcnt << endl;
	for(int i = 0;i < fcnt;i++){
		printf("%d-%02d-%02d\n",data[i].year,data[i].month,data[i].day);
	}
}

int main(){
	solve();
	return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值