小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在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
思路:模拟
一个输入大概有三种情况,逐一进行判断即可.由于在最后输出是按照日期从早到晚,所以得对其先排序再输出.可以把日期变成一个字符串然后放到set中 对其默认升序排序,然后作为键存到map中,值就是我们输出的字符串.最后遍历set输出map中的键即可.
在这个过程中用到了:
闰年的判断.
字符串和整形的相互转换,利用stringstream
字符和整形数据的相互转化: char=> int 6 = ‘6’ - ‘0’ int => char 强制转化 (int)ch
参考代码
#include<bits/stdc++.h> #include<sstream> using namespace std; string str; int AA,BB,CC; map<string,string> mp; set<string> jh; int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; string change(int m) { stringstream sm; sm.clear(); string str2,str3; if(m<10) { sm<<m; sm>>str2; str3 = "0"+str2; } else { sm<<m; sm>>str3; } return str3; } bool judgeRN(int year) { //判断是否是闰年. if((year % 4==0&&year % 100!=0 )||(year % 400 == 0)) { return true; } else { return false; } } void solve(int year,int month,int day) { if(judgeRN(year)) { months[2] = 29; } else { months[2] = 28; } if(month>0&&month<=12&&day >0&&day <= months[month]) { stringstream sm; string str2,str3; sm.clear(); if(year <= 59) { sm<<(year+2000); sm>>str2; } else { sm<<(year+1900); sm>>str2; } str3 = str2+ change(month)+change(day); jh.insert(str3); mp[str3] = str2+"-"+change(month)+"-"+change(day); } } int main() { //年份 如果是0-59说明是 20+年 否则是19+年 月:1-12 日: 可能是28,29,30,31 // 1.AA/BB/CC =>> 分三种: AA/BB/CC CC/AA/BB CC/BB/AA 进行枚举,判断 //2.年数 考虑范围.可能有两种. cin>>str;//01AA 34BB 67CC AA = (int)(str[0]-'0')*10+(str[1]-'0'); BB = (str[3]-'0')*10+(str[4]-'0'); CC = (str[6]-'0')*10+(str[7]-'0'); solve(AA,BB,CC); solve(CC,AA,BB); solve(CC,BB,AA); for(set<string>::iterator it = jh.begin(); it !=jh.end(); it++) { cout<<mp[*it]<<endl; } return 0; }