zoukankan      html  css  js  c++  java
  • LightOJ

    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

    题意:输出比X大的第一个回文串。

    思路:做过很多次了感觉,还是WA了几发。 按照如下几个步骤。

    1,我们先按照左边几位对称到右边,如果比原串大,输出。

    2,从中间到左边找第一个非‘9’的字符,+1,中间的全部变为‘0’。

    3,全部都是‘9’,则长度+1,首尾为‘1’,其他为‘0’;

    #include<bits/stdc++.h>
    #define ll long long
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    const int maxn=100010;
    char d[maxn],a[maxn];int cnt;
    void put(int len)
    {
        if(len==-1){
            putchar('1'); rep(i,1,cnt-1) putchar('0');
            putchar('1'); putchar('
    '); return ;
        }
        rep(i,1,len) putchar(a[i]); putchar('
    ');
    }
    void cal()
    {
        cnt=strlen(d+1);
        rep(i,1,cnt/2) swap(d[i],d[cnt+1-i]);
        rep(i,cnt/2+1,cnt) a[i]=d[i];
        rep(i,1,cnt/2) a[i]=a[cnt+1-i];
        for(int i=cnt;i>=1;i--)
          if(a[i]>d[i]){
             put(cnt); return ;
          }
        else if(a[i]<d[i]) break;
        rep(i,cnt/2+1,cnt){
            if(a[i]<'9') {
                a[i]++;
                rep(j,cnt-i+2,i-1) a[j]='0';
                a[cnt+1-i]=a[i];
                put(cnt); return ;
            }
        }
        put(-1); return ;
    }
    int main()
    {
        int T,C=0,x;
        scanf("%d",&T);
        while(T--){
            scanf("%s",d+1);
            printf("Case %d: ",++C);
            cal();
        }
        return 0;
    }
  • 相关阅读:
    Python学习笔记_从CSV读取数据写入Excel文件中
    Python学习笔记_Python向Excel写入数据
    Python学习笔记_一个Tkinter示例,使用FileDialog
    Python学习笔记_我的参考网址
    Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal multibyte sequence
    Python读取CSV文件
    JS写的多级联select,如何取值
    c#常用的Datable转换为json,以及json转换为DataTable操作方法
    C# 读写App.config
    一个简单的存储过程使用事务的例子
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9981311.html
Copyright © 2011-2022 走看看