I - Palindromes 打表找规律

题目大意:

       给出k,让求出第k个回文数(k的“长度”不超过1e5)

题解:

       真是一道给人警醒的题目

       谁说数据范围大就要用Java!还有可能是找规律!

       队友写java,构造,写了四个小时。

       如果我也去一起看I题,如果我能质疑一下他们的思路,自己找规律看一下。。。。。。诶

 

     打表仔细耐心看一下是可以发现规律的

     ①对于个位数和10,11就特判一下

      ②然后对于首个数字不是1的k,它对应的回文串就是将首数字减一,在反转顺序贴到后面

            如k=523,则它对应的回文串就是42324

          (注意,这种情况下,一定是将0-len-1位反转贴到后面,即中间要留一个)

      ③对于首个数字是1的k,是将它的首数字1直接抛弃,选取第2-len位

             如果第2位数字不是0,就是将2-len位反转后贴到后面

            如果第2位数字是0,先将0替换成9,再将2~len-1位反转后贴到后面(即这时中间要留一个)

打表代码

 1 #include <bits/stdc++.h>
 2 #include <cstring>
 3 #include<string>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 #include <stdlib.h>
 7 #include <stdio.h>
 8 using namespace std;
 9 #define ll long long
10 bool pd(ll x)
11 {
12     ostringstream oss;
13     oss<<x;
14     string s=oss.str();
15     int len=s.length();
16     for(int i=0; i<len/2; ++i)
17         if(s[i]!=s[len-i-1])
18             return 0;
19     return 1;
20 }
21 int main()
22 {
23     //freopen("output.txt","w",stdout);
24     ll cnt=0;
25     for(ll i=0; i<=100000000000000000; ++i)
26         if(pd(i))
27             cout<<i<<"------"<<++cnt<<endl;
28     return 0;
29 }

ac代码

 1 #include <bits/stdc++.h>
 2 #include <cstring>
 3 #include<string>
 4 using namespace std;
 5 #define ll long long
 6 int main()
 7 {
 8     int T,len;
 9     string s,t,tt;
10     cin>>T;
11     while(T--)
12     {
13         cin>>s;
14         len=s.length();
15         if(len==1 || s=="11" || s=="10")
16         {
17             if(s=="11")
18                 puts("11");
19             else if(s=="10")
20                 puts("9");
21             else
22                 cout<<(char)(s[0]-1)<<endl;
23             continue;
24         }
25         if(s[0]=='1')
26         {
27             if(s[1]=='0')
28             {
29                 s[1]='9';
30                 s=s.substr(1,len-1);
31                 t=s.substr(0,len-2);
32                 reverse(t.begin(),t.end());
33                 s+=t;
34                 cout<<s<<endl;
35             }
36             else
37             {
38                 s=s.substr(1,len-1);
39                 t=s;
40                 reverse(t.begin(),t.end());
41                 s+=t;
42                 cout<<s<<endl;
43  
44             }
45             continue;
46         }
47         s[0]-=1;
48         t=s.substr(0,len-1);
49         reverse(t.begin(),t.end());
50         s+=t;
51         cout<<s<<endl;
52     }
53     return 0;
54 }

 

posted @ 2020-09-26 22:32  古比  阅读(153)  评论(0编辑  收藏  举报