简体   繁体   English

给定回文数之前的先前最大回文数

[英]previous greatest palindrome numbers before a given palindrome number

The Question is: You are given a number 'N' in the form of a string 'S', which is a palindrome.问题是:给你一个数字“N”,以字符串“S”的形式出现,这是一个回文。 You need to find the greatest number strictly less than 'N' which is also a palindrome.您需要找到严格小于“N”的最大数,这也是一个回文。

I tried to solve this question but is giving wrong answer for some test cases.我试图解决这个问题,但对某些测试用例给出了错误的答案。 can anyone help me to correct my code.谁能帮我纠正我的代码。 Below is my code:下面是我的代码:

string nextSmallerPalindrome(string &s)
{
    int n = s.length();
    string ans = "";
    
    if(n == 1)
    {
        s[0]--;
        return s;
    }
    
    if(s == "11")
    {
        return "9";
    }
    
   // For Handling odd cases
    if(n % 2 != 0)
    {
        int idx = n / 2;
        int diff = 0;
        
        if(s[idx] == '0')
        {
            s[idx] = '9';
            diff = 1;
        }
        else
        {
            s[idx]--;
        }
        
        idx--;
        
        while(idx >= 0 && diff == 1)
        {
            if(s[idx] == '0')
            {
                s[idx] = '9';
                idx--;
            }
            else
            {
                s[idx]--;
                diff = 0;
                break;
            }
        }
        
        int i = 0;
        while(i < n && s[i] == '0')
        {
            i++;
        }
        
        for(; i < n; i++)
        {
            ans = ans + s[i];
        }
        
        int new_n = ans.length();
        
        int j = 0;
        int k = new_n - 1;
        
        while(j < k)
        {
            if(ans[j] == ans[k])
            {
                j++;
                k--;
            }
            else
            {
                ans[k] = ans[j];
                j++;
                k--;
            }
        }
        
        return ans;
    }
    else                             // For handling even cases
    {
        int idx = n / 2 - 1;
        int diff = 0;
        
        if(s[idx] == '0')
        {
            s[idx] = '9';
            diff = 1;
        }
        else
        {
            s[idx]--;
        }
        
        idx--;
        
        while(idx >= 0 && diff == 1)
        {
            if(s[idx] == '0')
            {
                s[idx] = '9';
                idx--;
            }
            else
            {
                s[idx]--;
                diff = 0;
                break;
            }
        }
        
        int i = 0;
        while(i < n && s[i] == '0')   // For ignoring Zeros from front of the string
        {
            i++;
        }
        
        for(; i < n; i++)     //storing all the string s in new string ans after ignoring front 0
        {
            ans = ans + s[i];
        }
        
        int new_n = ans.length();
        
        int j = 0;
        int k = new_n - 1;
        
        while(j < k)                // checking and changing the last half into first half
        {
            if(ans[j] == ans[k])
            {
                j++;
                k--;
            }
            else
            {
                ans[k] = ans[j];
                j++;
                k--;
            }
        }
        
        return ans;
    }
}

Input Format: The first line of the input contains an integer T denoting the number of test cases.输入格式:输入的第一行包含一个 integer T 表示测试用例的数量。

The first and the only line of each test case contains a string 'S', denoting the number whose next smaller palindrome is to be found.每个测试用例的第一行也是唯一一行包含一个字符串“S”,表示要找到其下一个较小回文的数字。

Test Case:测试用例:

19
7
77
101
1001
1221
144441
3444444443
57855875
10000001
11
1
111
101
1001
11011
1110111
1190911
20002
10011001

Excuse me, but I will not fix the code.对不起,我不会修复代码。 I will rather describe how I would solve it.我宁愿描述我将如何解决它。 If you like the idea, then you will have a very easy time doing it.如果您喜欢这个想法,那么您将很容易做到。

A palindrome has n digits.回文数有 n 个数字。 n may be pair or odd. n 可以是对的或奇数的。 The first n / 2 digits (rounded upwards) is a number.前 n / 2 位数字(向上舍入)是一个数字。 Get that number (cut down the digits in the second half) and subtract 1.得到那个数字(减少下半部分的数字)并减去 1。

Look at the result and the number of its digits.查看结果及其位数。 If subtracting 1 decreases the number of digits of your half-number, then handle that accordingly.如果减去 1 会减少半数的位数,请相应地处理。 Should be easy if you have the right idea.如果您有正确的想法,应该很容易。

Seems like using the same expression for idx in even cases as odd cases works better.似乎在偶数情况下对 idx 使用相同的表达式,因为奇数情况效果更好。

(It still doesn't handle the case for 7 , though.) (不过,它仍然不能处理7的情况。)

Change this:改变这个:

else                             // For handling even cases
{
    int idx = n / 2 - 1;

to this:对此:

else                             // For handling even cases
{
    int idx = n / 2;

Results:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM