(codechef)The Next Palindrome

A positive integer is called a palindrome if its representation in the
decimal system is the same when read from left to right and from right
to left. For a given positive integer K of not more than 1000000
digits, write the value of the smallest palindrome larger than K to
output. Numbers are always displayed without leading zeros. Input The
first line contains integer t, the number of test cases. Followed by t
lines containing integers K. Output For each K, output the smallest
palindrome larger than K. Example Input:

2

808

2133 Output:

818

2222

模拟,情况要考虑周到。
999 192 9999 …
这些容易错的情况注意下应该没什么问题。
CODE:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double PI=acos(-1);
const int N=1e4;
int t,fla,ffla; 
// ffla字符串中间的数是否为9,并且将奇数长度的字符串和偶数长度的字符串分开考虑
// fla 记录在改变除中间两个数之外的其他数时,整个字符串的大小是变大还是变小,还是没有变。
int main()
{
    string str;
    cin>>t;
    while(t--){
        fla=-1;ffla=0;
        cin>>str;
        int len = str.length();
        int N=len/2;
        if(len%2==0) N--;
        for(int i=0;i<N;i++){
            if(str[i]!=str[len-1-i]){
                if(str[len-1-i]>str[i])
                    fla=0;
                else fla=1;
                str[len-1-i]=str[i];
            }
        }
        if(len%2==1){
            if(fla==-1||fla==0)
                str[len/2]++;
            if(str[len/2]>'9')
                ffla=1;
        }
        else{
            if(str[len/2-1]==str[len/2])
            {
                if(fla==-1||fla==0)
                    str[len/2-1]=str[len/2]=str[len/2-1]+1;
            }
            else if(str[len/2-1]<str[len/2]){
                str[len/2-1]=str[len/2]=str[len/2-1]+1;
            }
            else{
                str[len/2]=str[len/2-1];
            }
            if(str[len/2]>'9')
                ffla=-1;
        }

        //
        if(ffla==1)
        {
            str[len/2]='0';
            int yu=1;
            for(int i=len/2-1;i>=0;i--){
                str[i]=str[i]+yu;
                if(str[i]>'9')
                    str[i]='0';
                else {
                    yu=0;
                    break;
                }
            }
            if(yu){
                str[0]='0';
                str.insert(0,1,'1');
            }
            int nowlen=str.length();
            for(int i=0;i<nowlen/2;i++){
                str[nowlen-1-i]=str[i];
            }
        }
        else if(ffla==-1)
        {
            str[len/2-1]=str[len/2]='0';
            int yu = 1;
            for(int i=len/2-2;i>=0;i--){
                str[i]+=1;
                if(str[i]>'9')
                    str[i]='0';
                else{
                    yu=0;
                    break;
                }
            }
            if(yu){
                str[0]='0';
                str.insert(0,1,'1');
            }
            int nowlen=str.length();
            for(int i=0;i<nowlen/2;i++){
                str[nowlen-1-i]=str[i];
            }
        }

        cout<<str<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值