Palindromic Numbers (III)(回文数,较麻烦)

Palindromic Numbers (III)

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

Vinci is a little boy and is very creative. One day his teacher asked him to write all the Palindromic numbers from 1 to 1000. He became very frustrated because there is nothing creative in the task. Observing his expression, the teacher replied, "All right
then, you want hard stuff, you got it." Then he asks Vinci to write a palindromic number which is greater than the given number. A number is called palindromic when its digits are same from both sides. For example: 1223221, 121, 232 are palindromic numbers
but 122, 211, 332 are not. As there can be multiple solutions, Vinci has to find the number which is as small as possible.

Input

Input starts with an integer T (≤ 30), denoting the number of test cases.

Each case starts with a line containing a positive integer. This integer can be huge and can contain up to 105 digits.

Output

For each case, print the case number and the minimum possible palindromic number which is greater than the given number.

Sample Input

5

121

1

1332331

11

1121

Sample Output

Case 1: 131

Case 2: 2

Case 3: 1333331

Case 4: 22

Case 5: 1221

Hint

Dataset is huge, use faster I/O methods.

AC CODE:

//Memory: 1608 KB		Time: 82 MS
//Language: C++		Result: Accepted

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

char s[100001];

int main()
{
    int T, c = 1;
    int i, j, len, mid;
    bool odd, flag;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s", s);
        printf("Case %d: ", c++);
        //特判所有digit为9的情况
        for(i = 0; s[i] == '9'; i++){}
        if(s[i] == '\0')
        {
            putchar('1');
            for(i = 0; s[i+1] != '\0'; i++)
                putchar('0');
            puts("1");
            continue;
        }
        len = strlen(s);
        //特判只有一位数的情况
        if(len < 2)
        {
            s[0]++;
            puts(s);
            continue;
        }
        mid = len / 2;
        odd = len & 1;
        if(odd)
        {
            for(i = mid - 1, j = mid + 1; i > -1 && s[i] == s[j]; i--, j++){}
            //原数是回文数
            if(i == -1)
            {
                for(i = mid; s[i] == '9'; i++)
                    s[len - i - 1] = s[i] = '0';
                s[len - i - 1] = ++s[i];
                puts(s);
            }
            else if(s[i] > s[j])
            {
                for(i = 0; i < mid; i++)
                    putchar(s[i]);
                for(; i > -1; i--)
                    putchar(s[i]);
                puts("");
            }
            //s[i] < s[j]
            else
            {
                /*for loop中的判断条件不需要i > -1,因为必定能找到s[i] != '9'的i。试想如果
                s[i] == '9'一直成立,就不会有s[i] < s[j]了。*/
                for(i = mid; s[i] == '9'; i--)
                    s[i] = '0';
                s[i]++;
                for(i = 0; i < mid; i++)
                    putchar(s[i]);
                for(; i > -1; i--)
                    putchar(s[i]);
                puts("");
            }
        }
        //odd = false
        else
        {
            for(i = mid - 1, j = mid; i > -1 && s[i] == s[j]; i--, j++){}
            //原数是回文数
            if(i == -1)
            {
                /*此处for loop不需要i<len这一判断条件,因为此前已特判所有digit为9的情况,故
                必然存在i使得s[i]!='9'*/
                for(i = mid; s[i] == '9'; i++)
                    s[len - i - 1] = s[i] = '0';
                s[len - i - 1] = ++s[i];
                for(i = 0; i < len; i++)
                    putchar(s[i]);
                puts("");
            }
            else if(s[i] > s[j])
            {
                for(i = 0; i < mid; i++)
                    putchar(s[i]);
                for(i = mid - 1; i > -1; i--)
                    putchar(s[i]);
                puts("");
            }
            //s[i] < s[j]
            else
            {
                //必定存在i使得s[i]!='9',不然不会有s[i] < s[j](这是进入这个else的条件)
                for(i = mid - 1; s[i] == '9'; i--)
                    s[i] = '0';
                s[i]++;
                for(i = 0; i < mid; i++)
                    putchar(s[i]);
                for(i = mid - 1; i > -1; i--)
                    putchar(s[i]);
                puts("");
            }
        }
    }
}

原文链接: https://www.cnblogs.com/cszlg/archive/2012/08/06/2910574.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Palindromic Numbers (III)(回文数,较麻烦)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/58280

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午8:34
下一篇 2023年2月9日 上午8:34

相关推荐