问题
给一个数k,给出第k个回文数 链接
题解
打表找规律,详见https://www.cnblogs.com/lfri/p/10459982.html,差别仅在于这里从1数起.
AC代码
#include<cstdio> #include<iostream> #include<string> #include<sstream> using namespace std; typedef long long LL; void solve(string str) { int len = str.length(); if (len == 1) { printf("%c ", str[0] - 1); return; } if (str[0] == '1') { if (str[1] == '0') { str[1] = '9'; for (int i = 1; i < len; i++) printf("%c", str[i]); for (int i = len - 2; i >= 1; i--) printf("%c", str[i]); printf(" "); } else { for (int i = 1; i < len; i++) printf("%c", str[i]); for (int i = len - 1; i >= 1; i--) printf("%c", str[i]); printf(" "); } } else { str[0] = str[0] - 1; for (int i = 0; i < len; i++) printf("%c", str[i]); for (int i = len - 2; i >= 0; i--) printf("%c", str[i]); printf(" "); } } void ToString(LL num, string& str) { stringstream ss; ss << num; ss >> str; } string str; LL num; int main() { while (cin >> num && num) { ToString(num + 1, str); //从1开始num要加一,从0开始不用 solve(str); } return 0; }