zoukankan      html  css  js  c++  java
  • PAT 天梯赛 L1-050. 倒数第N个字符串 【字符串】

    题目链接

    https://www.patest.cn/contests/gplt/L1-050

    思路
    因为是求倒数 我们不如直接 倒过来看 令 zzz 为第一个字符串
    我们可以理解为 十进制 转换为 二十六进制

    我们分别令

    z, y, x …… a 为 25 , 24 , 23 ……….. 0

    然后我们知道 第一个字符串 zzz 代表的数字是 0 但是它是第一个字符串
    所以 这里 是相差一位的 我们不如直接 将 zzz 表示为 第0个字符串 那么给的N 就要相应的 - 1

    然后进制转换 用字母来表示 就可以了

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    #define pb push_back
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    
    const double PI  = 3.14159265358979323846264338327;
    const double E   = exp(1);
    const double eps = 1e-6;
    
    const int INF  = 0x3f3f3f3f;
    const int maxn = 1e5 + 5;
    const int MOD  = 1e9 + 7;
    
    int main()
    {
        map <int, char> m;
        for (int i = 'z', j = 0; i >= 'a'; i--, j++)
            m[j] = i;
        int a, b;
        scanf("%d%d", &a, &b);
        b--;
        int ans[6];
        CLR(ans);
        for (int i = 0; b; i++)
        {
            ans[i] = b % 26;
            b /= 26;
        }
        string s = "";
        for (int i = 0; i < a; i++)
            s += m[ans[i]];
        reverse(s.begin(), s.end());
        cout << s << endl;
    }
  • 相关阅读:
    java.lang.NoClassDefFoundError: org.junit.runner.Runner
    SpringMVC 八大注解
    spring @autowired使用总结
    Git使用入门
    N21_栈的压入弹出序列。
    N20_包含min函数的栈
    N19_顺时针打印指针
    N17_判断树B是不是树A的子结构
    N16_合并两个排序链表
    N15_反转链表后,输出新链表的表头。
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433167.html
Copyright © 2011-2022 走看看