zoukankan      html  css  js  c++  java
  • 2018 北京区域赛 I

    题目

     HihoCoder - 1878

    题目大意

     给出k,让求出第k个回文数(k的“长度”不超过1e5)

    题解

    之前做过类似的题,是统计各阶段的数找到第K个回文数,但这里K太大,需要寻找新的方法。

    打表找规律:

    只有一位数:减一输出

    否则:

             若第0位为2~9 :首位减一,0~len-2反转贴后面

             若第0位为1:若第1位为1~9:丢掉首位,剩下的反转贴后面

                                   若第1位为0:丢掉首位,第1位改成9,2~len-2反转贴后面.

    AC代码

    #include<cstdio>
    #include<iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    
    void solve(string str)
    {
        int len = str.length();
    
        if (len == 1)
        {
            printf("%c
    ", str[0] - 1);
            return;
        }
    
        if (str[0] == '1')
        {
            if (str[1] == '0')
            {
                str[1] = '9';
                for (int i = 1; i < len; i++)  printf("%c", str[i]);
                for (int i = len - 2; i >= 1; i--)  printf("%c", str[i]);
                printf("
    ");
            }
            else
            {
                for (int i = 1; i < len; i++)  printf("%c", str[i]);
                for (int i = len - 1; i >= 1; i--)  printf("%c", str[i]);
                printf("
    ");
            }
        }
        else
        {
            str[0] = str[0] - 1;
            for (int i = 0; i < len; i++)  printf("%c", str[i]);
            for (int i = len - 2; i >= 0; i--)  printf("%c", str[i]);
            printf("
    ");
        }
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while (T--)
        {
            string str;
            cin >> str;
            solve(str);
        }
    }

    打表程序

    #include<cstdio>
    #include<iostream>
    #include<sstream>
    #include<string>
    using namespace std;
    
    typedef long long LL;
    
    const LL maxn = 1e18;
    
    void ToString(LL num, string& str)
    {
        stringstream ss;
        ss << num;
        ss >> str;
    }
    
    bool ispd(string str)
    {
        bool flag = true;
        int len = str.length();
        for (int i = 0; i < len / 2; i++)
            if (str[i] != str[len - 1 - i])  flag = false;
        return flag;
    }
    
    int main()
    {
        LL cnt = 1;
        for (LL i = 0; i < maxn; i++)
        {
            string s;
            ToString(i, s);
            if (ispd(s))  cout << cnt++ << ' ' << i << endl;
        }
        return 0;
    }
    View Code

    参考链接:https://blog.csdn.net/hxxjxw/article/details/84205963

  • 相关阅读:
    JQuery框架中使用blockUI制作自定义的漂亮的网页提示框
    PHP树形菜单一次展开一个子项目,可以记录打开的项目,刷新后不变
    第一次面试
    东软的校园招聘笔试
    fscommand
    从 ActionScript 中调用外部代码
    GCC 参数详解
    flash build找不到调试版plashplayer的解决办法
    C# winform与 flash as 的交互通讯
    LLVM 与 Clang 介绍
  • 原文地址:https://www.cnblogs.com/lfri/p/10459982.html
Copyright © 2011-2022 走看看