题目链接:
https://www.acwing.com/problem/content/1231/
题解:
估计自己太弱了,处理字符串比较痛苦,最后记得用unique去重。
代码:
#include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <sstream> using namespace std; int months[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; bool check(int year,int mm,int dd){ if(mm < 1 || mm > 12) return false; if( (mm != 2 && dd > months[mm]) || dd < 1) return false; if(mm == 2){ int lamp = (year % 100 && year % 4 == 0) || year % 400 == 0; if(dd > months[mm] + lamp || dd < 1) return false; } return true; } const int N = 8; int len = 0; string ss[N]; int main(){ string s; cin >> s; int n1 = (s[0]-'0')*10+(s[1]-'0'); int n2 = (s[3]-'0')*10+(s[4]-'0'); int n3 = (s[6]-'0')*10+(s[7]-'0'); // cout << n1 << " " << n2 << " " << n3 << endl; if(n1 <= 59){ int year = 20*100+n1; if(check(year,n2,n3)){ stringstream strss; string mm = to_string(n2); if(n2 < 10) mm = "0" + mm; string dd = to_string(n3); if(n3 < 10) dd = "0" + dd; string yyyy = to_string(year); strss << year << "-" << mm << "-" << dd; string res = strss.str(); ss[len] = res; len++; } }else{ int year = 19*100+n1; if(check(year,n2,n3)){ stringstream strss; string mm = to_string(n2); if(n2 < 10) mm = "0" + mm; string dd = to_string(n3); if(n3 < 10) dd = "0" + dd; string yyyy = to_string(year); strss << year << "-" << mm << "-" << dd; string res = strss.str(); ss[len] = res; len++; } } if(n3 <= 59){ int year = 20*100+n3; if(check(year,n1,n2)){ stringstream strss; string mm = to_string(n1); if(n1 < 10) mm = "0" + mm; string dd = to_string(n2); if(n2 < 10) dd = "0" + dd; string yyyy = to_string(year); strss << year << "-" << mm << "-" << dd; string res = strss.str(); ss[len] = res; len++; } if(check(year,n2,n1)){ stringstream strss; string mm = to_string(n2); if(n2 < 10) mm = "0" + mm; string dd = to_string(n1); if(n1 < 10) dd = "0" + dd; string yyyy = to_string(year); strss << year << "-" << mm << "-" << dd; string res = strss.str(); ss[len] = res; len++; } }else{ int year = 19*100+n3; if(check(year,n1,n2)){ stringstream strss; string mm = to_string(n1); if(n1 < 10) mm = "0" + mm; string dd = to_string(n2); if(n2 < 10) dd = "0" + dd; string yyyy = to_string(year); strss << year << "-" << mm << "-" << dd; string res = strss.str(); ss[len] = res; len++; } if(check(year,n2,n1)){ stringstream strss; string mm = to_string(n2); if(n2 < 10) mm = "0" + mm; string dd = to_string(n1); if(n1 < 10) dd = "0" + dd; string yyyy = to_string(year); strss << year << "-" << mm << "-" << dd; string res = strss.str(); ss[len] = res; len++; } } sort(ss,ss+len); len = unique(ss,ss+len) - ss; // cout << len << endl; for(int i=0;i<len;i++) cout << ss[i] << endl; }