Heng_Xin 题解分享 · 2024/4/4
回文日期(编程题) - 题解
使用视频day1的日期模版, 即可❤️ ```C++ #include <cstdio> #include <string> using namespace std; int getInt(char *s, int l, int r) { int res = 0; while (l <= r) { res = res * 10 + s[l] - '0'; ++l; } return res; } int main() { char str[9]; char s[9]; scanf("%s", s); int yyyy = getInt(s, 0, 3); int mm = getInt(s, 4, 5); int dd = getInt(s, 6, 7); bool tag = 0; bool pt = 0, tt = 0; string res_1, res_2; for (int y = yyyy; y <= 89990; ++y) { for (int m = 1; m <= 12; ++m) { for (int d = 1; d <= 31; ++d) { // 这里是模版 if (!(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)) { if (m == 2) { // 闰年: 可以被400整除 或者 被4而不能被100整除的年 if (m % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) { // 如果是润年 if (d > 29) break; } else { if (d > 28) break; } } else { if (d > 30) break; } } // 注意, 这一天不算,需要跳过 if (!tag && y == yyyy && m == mm && d == dd) { tag = 1; continue; } if (!tag) continue; // 这里是需要模拟做的事情 sprintf(str, "%04d%02d%02d", y, m, d); // 输出回文数 if (!pt) { string s1(str); string s2(s1.rbegin(), s1.rend()); if (s1 == s2) { res_1 = s1; pt = 1; if (tt) goto A; } } // 判断是否是回文数 即 // ABABBABA // 01234567 if (str[0] == str[2] && str[2] == str[5] && str[5] == str[7] && str[1] == str[3] && str[3] == str[4] && str[4] == str[6]) { res_2 = string(str); tt = 1; if (pt) goto A; } } } } A: // 题目要求的输出顺序 printf("%s\n%s\n", res_1.c_str(), res_2.c_str()); return 0; } ```
查看全文
0 0 1 1
astronomer 题解分享 · 2025/3/25
回文日期(编程题) - 题解
include using namespace std; int s2i(string s) { int res=0; for(const auto &i:s) res=res10+i-'0'; return res; } string i2s(int x,int w) { string res; while(x)res+=(x%10)+'0',x/=10; while(res.length() >s; int year=s2i(s.substr(0,4)),month=s2i(s.substr(4,2)),day=s2i(s.substr(6,2)); ``` bool ans1=false,ans2=false; for(int i=year;i<=9999;i++) { for(int j=1;j<=12;j++) { if(i==year&&j<month)continue; for(int k=1;k<=31;k++) { if(i==year&&j==month&&k==day)continue; if(!isok(i,j,k))continue; string date=i2s(i,4)+i2s(j,2)+i2s(k,2); if(!ans1&&ispa(date)) { cout<<date<<endl; ans1=true; } if(!ans2&&ispa2(date)) { cout<<date<<endl; ans2=true; } } } } return 0; ``` }
查看全文
0 0 0 2
yuri01 题解分享 · 2024/12/18
回文日期(编程题) - 题解
把年份反转作为月日再判断是否合法 ```cpp // https://dashoj.com/d/lqbproblem/p/110 #include <bits/stdc++.h> using namespace std; int months[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int yy, mm, dd; string str; bool b1 = true, b2 = true; int main() { getline(cin, str); // 20200202 string stry = str.substr(0, 4); // "2020" yy = stoi(stry); // 2020 for (int y = yy + 1; y <= 9999; y++) { if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) // 判润 months[2] = 29; else months[2] = 28; stry = to_string(y); string yn = stry; // 反转的 年 reverse(yn.begin(), yn.end()); mm = stoi(yn.substr(0, 2)); // 反转后的 月 dd = stoi(yn.substr(2, 2)); // 反转后的 日 if (mm <= 12) { if (dd <= months[mm]) { if (stry[0] == stry[2] && stry[1] == stry[3] && b2) { printf("%d%02d%02d\n", y, mm, dd); b2 = false; } if (b1) { printf("%d%02d%02d\n", y, mm, dd); b1 = false; } } } } return 0; } ```
查看全文
0 0 0 3
Mandy 题解分享 · 2025/1/16
回文日期(编程题) - 题解
``` #include <bits/stdc++.h> using namespace std; bool isValid(int date){//判断日期是否合法 int year = date/10000; int month = (date%10000)/100; int day = date%100; if(month <1 || month>12) return false; if(day<1) return false; if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){ if(day>31) return false; }else if(month==4 || month==6 || month==9 || month==11){ if(day>30) return false; }else if(month==2){ if((year%4==0 && year%100!=0) || year%400==0) { if(day>29) return false; }else{ if(day>28) return false; } } return true; } bool is\_huiwen(string str)//判断是否是回文数 { string temp = str; reverse(temp.begin(),temp.end()); return str==temp; } bool is\_abab(string str)//判断是否是abab型数 { return str[0] == str[2] && str[1] == str[3] && is\_huiwen(str); } int main() { int n; cin>>n; string first\_huiwen,first\_abab; while(1) { n++; if(!isValid(n)) continue; string str = to\_string(n); if(is\_huiwen(str)&&first\_huiwen.empty()){ first\_huiwen = str; } if(is\_abab(str)&&first\_abab.empty()){ first\_abab = str; } if(!first\_huiwen.empty()&&!first\_abab.empty()){ break; } } cout<<first\_huiwen<<endl<<first\_abab<<endl; return 0; } ```
查看全文
0 0 0 1
Captain_Dong 题解分享 · 2024/4/11
回文日期(编程题) - 题解
``` #include<bits/stdc++.h> using namespace std; int months[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //判断日期合法性 bool check_date(int date){ //date=2014 10 23 int year = date/10000; //分离出年 -->2014 int month = date%10000/100; //分离出月 %10000后:1023 再/100后-->10 int day = date%100; //分离出日 --> 23 //判断所拿到的数据是否合法 if(!day||month<=0||month>12) return false; if(month !=2 && day>months[month]) return false; //如果是2月 判断闰年情况 if(month==2) if((year%4==0 && year%100!=0) || year%400==0){ //是闰年 if(day>29) return false; else{ if(day>28) return false; } } return true; } //判断是否是回文数 -->8位数--》头尾相等 bool check_huiwenshu(string str){ for(int i=0,j=(str.length()-1);i<j;i++,j--){ ////两个变量当两个指针, 一个从头一个从尾用if判断 if(str[i] != str[j]) return false; } return true; } //判断回文数是否是ABABBABA型 bool check_ABABBABA(string str){ //先判断是否是回文数 if(check_huiwenshu(str)){ //在判断是否是 ABABBABA型 if(str[0] != str[2] || str[1] != str[3]) return false; return true; } return false; } int main(){ int date,flag=0; cin>>date; for(int i=date+1;;i++){ if(check_date(i)){ //判断日期是否合法 string str=to_string(i); ////判断完后将年月日从数字转化为字符串,方便进行回文判断 if(check_huiwenshu(str) && !flag){ //判断是否是回文数 cout<<i<<endl; flag=1; } if(check_ABABBABA(str)){ //判断是否是ABABBABA型 cout<<i<<endl; return 0; } } } return 0; } ```
查看全文
0 0 0 2
EternalY 题解分享 · 2024/4/9
回文日期(编程题) - 题解
使用日期问题模板 ``` #define #include <bits/stdc++.h> #include <algorithm> using namespace std; void fun1(int date1, char a[], int n1, int n2) { for (int year = n1 + 1; year <= n2; year++) { for (int month = 1; month <= 12; month++) { for (int day = 1; day <= 31; day++) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { ; } else if (month == 2) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (day > 29) break; } else { if (day > 28) break; } } else { if (day > 30) { break; } } sprintf(a, "%d%02d%02d", year, month, day); if ((a[0] == a[7]) && (a[1] == a[6]) && (a[2] == a[5]) && (a[3] == a[4])) { return; } } } } } void fun2(int date1, char a[], int n1, int n2) { for (int year = n1 + 1; year <= n2; year++) { for (int month = 1; month <= 12; month++) { for (int day = 1; day <= 31; day++) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { ; } else if (month == 2) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (day > 29) break; } else { if (day > 28) break; } } else { if (day > 30) { break; } } sprintf(a, "%d%02d%02d", year, month, day); char temp1 = a[0]; char temp2 = a[1]; if ((temp1 == a[2] && temp1 == a[5] && temp1 == a[7]) && (temp2 == a[3] && temp2 == a[4] && temp2 == a[6])) { return; } } } } } int main() { int date = 0; cin >> date; int n1 = date / 10000; int n2 = n1 + 1500; char a[9]; char b[9]; fun1(date, a, n1, n2); fun2(date, b, n1, n2); cout << a << endl << b; return 0; } ```
查看全文
0 0 0 2
jkjkjla 题解分享 · 2024/4/5
回文日期(编程题) - 题解
``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String a = scan.next(); scan.close(); String r; boolean flag=true; String i=a; while(true) { i = dateUp(i); r = new StringBuilder(i).reverse().toString(); if(r.equals(i)) { if(flag) { System.out.println(i); flag = false; } String ab = i.substring(0, 2); String ba = i.substring(2, 4); if(ab.equals(ba)) { System.out.println(i); break; } } } } public static int[] days = new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31}; //每次日期加一天 public static String dateUp(String t) { int i = Integer.parseInt(t); //天份 int d = i%100; i=i/100; //月份 int m = i%100; i=i/100; //年份 int y = i; if((y%400==0)||(y%4==0&&y%100!=0)) { //闰 days[2] = 29; }else { days[2] = 28; } if(d+1>days[m]) { if(m+1>12) { //年份上加1 y++; //月份归0 m=0; }else { //月份++ m++; //日归0 d=0; } }else { d++; } return ""+(y*10000+m*100+d); } } ```
查看全文
0 0 0 2
kaisyuantseng 题解分享 · 2024/4/11
回文日期(编程题) - 题解
```cpp #include <bits/stdc++.h> using namespace std; #define int long long // #define LL long long #define endl '\n' int months[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; bool is_leap(int year) { return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0; } bool check1(int date) { string s = to_string(date); for (int i = 0, j = 7; i < 4; i++, j--) if (s[i] != s[j]) return false; return true; } bool check2(int date) { string s = to_string(date); return (s[0] == s[2] and s[2] == s[5] and s[5] == s[7] and s[1] == s[3] and s[3] == s[4] and s[4] == s[6]); } void solve() { int n; cin >> n; int res1 = 0, res2 = 0; for (int i = n + 1; ; i++) { int year = i / 10000; int month = i / 100 % 100; int day = i % 100; if (month > 12 or month < 1 or day > 31 or day < 1) continue; if (is_leap(year)) months[2] = 29; else months[2] = 28; if (day > months[month]) continue; if (check1(i) and !res1) res1 = i; if (check2(i) and !res2) { res2 = i; break; } } cout << res1 << endl << res2; } signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve(); return 0; } ```
查看全文
0 0 0 1
hfl2222 题解分享 · 2024/4/10
回文日期(编程题) - 题解
include 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 > 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
Liopen 题解分享 · 2024/4/10
回文日期(编程题) - 题解
``` #include<cstdio> #include<iostream> using namespace std; int check(int s){ //数字回文 int i=0; while(s){ i=i*10+s%10; s/=10; } return i; } int main(){ int yearbegin=1921; int yearend=2022; int flag=0; int flag2=0; int s=0; cin>>s; int syear=s/10000; int smonth=(s %10000)/100; int sday=s%100; // cout<<syear<<" "<<smonth<<" "<<sday; for(int year=syear;1;year++) for(int month=1;month<=12;month++) for(int day=1;day<=31;day++){ if(month==1||month==3||month==5||month==7 ||month==8||month==10||month==12){ } else if(month==2){ if((year%4==0&&year%100!=0||year%400==0)){ if(day>29){ break; } } else{ if(day>28) break; } } else{ if(day>30) break; } if(year==syear&&month==smonth&&day==sday) { flag=1; continue; } if(flag==1){ if(flag2==0&&check(year*10000+month*100+day)==year*10000+month*100+day){ cout<<year*10000+month*100+day<<endl; flag2=1; //回文已出现 } if(flag2==1&&check(year*10000+month*100+day)==year*10000+month*100+day){ if(year/100==year%100){ //是否为ababbaba cout<<year*10000+month*100+day; return 0; } } } } return 0; } ```
查看全文
0 0 0 1