zoukankan      html  css  js  c++  java
  • 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("");
                }
            }
        }
    }



  • 相关阅读:
    行为模式-模板方法
    行为模式-策略模式
    行为模式-观察者 模式
    行为模式-责任链模式
    python-json
    mongo资料
    用with管理lock锁
    枚举类使用
    db2常见操作命令
    trancate immediate reuse storage
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910574.html
Copyright © 2011-2022 走看看