标题:日期问题
小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在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
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cstdio>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
//split函数
struct date{
int year;
int month;
int day;
date(int year,int month,int day):year(year),month(month),day(day){};
// < 代表升序 >代表降序
bool operator < (const date &x) const{
if(year==x.year){
cout<<1;
if(month==x.month){
return day < x.day;
}
return month < x.month;
}
return year < x.year;
}
};
bool comp(const date &a,const date &b){
if(a.year==b.year){
if(a.month==b.month){
return a.day < b.day;
}
return a.month < b.month;
}
return a.year < b.year;
}
void split(const string &s,const string &c,vector<string> &result){
typedef string::size_type string_size;
string_size pos2= s.find(c);
string_size pos1= 0;
while(std::string::npos!=pos2){
result.push_back(s.substr(pos1,pos2-pos1));
pos1=pos2+c.size();
pos2=s.find(c,pos1);
}
if(pos1!=s.length()){
result.push_back(s.substr(pos1));
}
}
bool isrn(int n){
if(n%4==0&&n%400!=0||n%100==0){
return true;
}else{
return false;
}
}
void print(const date &d){
printf("%02d-%02d-%02d\n", d.year, d.month, d.day);
}
int main(int argc, char** argv) {
string datestr="02/02/02";
vector<string> result;
vector<date> d;
split(datestr,"/",result);
int int_first_temp;
stringstream stream;
stream<<result[0];
stream>>int_first_temp;
int int_second_temp;
stringstream stream2;
stream2<<result[1];
stream2>>int_second_temp;
int int_third_temp;
stringstream stream3;
stream3<<result[2];
stream3>>int_third_temp;
int int_year_temp;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//判重
bool aflag=true;
if(result[0]==result[1]&&result[0]==result[2]){
aflag=false;
}
bool flag=true;
if(result[0]==result[1]){
flag=false;
}
if(int_first_temp>=60&&int_first_temp<=99){
string str_temp = "19" +result[0];
stringstream stream4;
stream4<<str_temp;
stream4>>int_year_temp;
}else{
string str_temp = "20" +result[0];
stringstream stream5;
stream5<<str_temp;
stream5>>int_year_temp;
}
if(isrn(int_year_temp)&&(int_second_temp>=0&&int_second_temp<=12)){
month[2]=29;
if(month[int_second_temp]>=int_third_temp){
d.push_back(date(int_year_temp,int_second_temp,int_third_temp));
}
}else{
if(month[int_second_temp]>=int_third_temp){
d.push_back(date(int_year_temp,int_second_temp,int_third_temp));
}
}
if(aflag){
if(int_third_temp>=60&&int_third_temp<=99){
string str_temp = "19" + result[2];
stringstream stream6;
stream6<<str_temp;
stream6>>int_year_temp;
}else{
string str_temp = "20" + result[2];
stringstream stream7;
stream7<<str_temp;
stream7>>int_year_temp;
}
if(isrn(int_year_temp)&&(int_second_temp>=0&&int_second_temp<=12)){
month[2]=29;
if(month[int_second_temp]>=int_first_temp){
d.push_back(date(int_year_temp,int_second_temp,int_first_temp));
}
}else{
if(month[int_second_temp]>=int_first_temp){
d.push_back(date(int_year_temp,int_second_temp,int_first_temp));
}
}
if(flag){
if(isrn(int_year_temp)&&(int_first_temp>=0&&int_first_temp<=12)){
month[2]=29;
if(month[int_first_temp]>=int_second_temp){
d.push_back(date(int_year_temp,int_first_temp,int_second_temp));
}
}else{
if(month[int_first_temp]>=int_second_temp){
d.push_back(date(int_year_temp,int_first_temp,int_second_temp));
}
}
}
}
sort(d.begin(),d.end(),comp);
vector<date>::iterator iter;
for(iter=d.begin();iter!=d.end();iter++){
print(*iter);
}
return 0;
}