返回题解分享
讨论 / 题解分享/ 帖子详情

回文日期(编程题) - 题解

#include<bits/stdc++.h>
using namespace std;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int flag;
bool check(int year){
if(year%400==0||year%100!=0&&year%4==0){
return true;
}else{
return false;
}
}
bool check1(string s){
if(s[7]==s[0]&&s[6]==s[1]&&s[5]==s[2]&&s[4]==s[3])
return true;
else
return false;
}
bool check2(string s) {
if (s[0] != s[2] || s[1] != s[3] || s[0] == s[1])
return false;//判断是否是ABABBABA型
return true;
}
int main(){
string str = "";
for(int i = 0;i < 4;i++){
char ch;
cin >> ch;
str += ch;
}
int year = stoi(str);
string t3;
cin >> t3;
str += t3;
for(int y = year;y <= 8999;y++){
if(check(y))
months[2] = 29;
else
months[2] = 28;
string date = to_string(y);
string t1 = date;
for(int m = 1;m <= 12;m++){
date = t1;
if(m<10)
date += "0";
date += to_string(m);
string t2 = date;
for(int d = 1;d <= months[m];d++){
if(d<10)
date += "0";
date += to_string(d);
if(check1(date)){
if(date == str){
date = t2;
continue;
}
if(!flag){
cout << date << endl;
flag++;
}
if(check2(date)){
cout << date << endl;
return 0;
}
}
date = t2;
}
}
}
return 0;
}
0 回复 0 转发 0 喜欢 1 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!