天梯赛-L1-050 倒数第N个字符串(15)

给定一个完全由小写英文字母组成的字符串等差递增序列,该序列中的每个字符串的长度固定为 L,从 L 个 a 开始,以 1 为步长递增。例如当 L 为 3 时,序列为 { aaa, aab, aac, ..., aaz, aba, abb, ..., abz, ..., zzz }。这个序列的倒数第27个字符串就是 zyz。对于任意给定的 L,本题要求你给出对应序列倒数第 N 个字符串。

输入格式:

输入在一行中给出两个正整数 L(2 ≤ L ≤ 6)和 N(≤10​5​​)。

输出格式:

在一行中输出对应序列倒数第 N 个字符串。题目保证这个字符串是存在的。

输入样例:

3 7417

输出样例:

pat

 比赛的时候忘记补a了...扣了两分...

#include<cstdio>
#include <cstring>
#include<algorithm>
#include <iostream>
#include <cmath>
//#include <stdio.h>
#define ll long long
using namespace std;

int main()
{
    
    int q,n;
    int b[100005];
    while(cin>>q>>n)
    {
        if(n == 0)
            return 0;
        else{
            int c = pow(26 ,q) - n;
            int tmp = 0;
            while(c!=0)
            {
                b[tmp++] = c % 26;
                c = c / 26;
            }
            if(tmp < q)//不够的话要补a
            {
                for(int i = 0;i < q - tmp;i++)
                    printf("a");
            }
            for(int i = tmp - 1 ;i >= 0;i--)
            {
                printf("%c",'a' + b[i]);
            }
            
            
            
            printf("\n");
        }
    }
    return 0;
}
//5 11881373

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/82191582