Title repeat
Given a string arithmetic entirely composed of lowercase letters increasing sequence, each string in the sequence is fixed to L, starting from the L a, in increments of 1. For example, when L is 3, the sequence {aaa, aab, aac, ..., aaz, aba, abb, ..., abz, ..., zzz}. The penultimate 27 string sequence is zyz. For any given L, this problem requires you to give the corresponding N penultimate sequence string.
Input formats:
In the given input row two positive integers L (2 ≤ L ≤ 6) and N (≦ 10
. 5
).
Output formats:
In the row corresponding to the output sequences of the penultimate N string. Topic ensure that this string exists.
Sample input:
3 7417
Sample output:
pat
answer
In fact, this is a 26-ary problem.
-
Taking into account only from L 2-6 so he spent a half-day strength if else want to judge them, and found that this can be cumbersome and error-prone, in theory, this is the simplest method possible.
-
We might as well sit down and think about it, a lot of trouble dealing with letters, numbers so we can deal with that for a deal of ascll code.
-
First, L-digit determines the overall likely scenario is 26 L kinds of topics make a reciprocal N-th, so the number should be the first positive 26 L - N number of time being credited x
-
L = the first digit (possibly consisting of the remaining number of bits) x / + 'a', to know here is that char ( 'a' + 1) = 'b'
-
In order to not interfere with the foregoing, x = (remaining bits may be composed of) x modulo of
the second bit = x / L number of bits (number of bits consisting of the remaining possible) + 'a'
…and many more
100 ++ AND
#include <iostream>
using namespace std;
int main()
{
char str[15];
int L,N;
cin>>L>>N;
int sum=1;
//根据位数决定有多少种可能
for(int i=0;i<L;i++)
{
sum*=26;
}
//倒数第N个,所以应该找的是第sum-N个
sum-=N;
//对于字符数组,从第一个开始访问,
for(int i=L-1,j=0;i>=0;i--,j++)
{
int tmpsum=1;
for(int k=0;k<i;k++)
{
tmpsum*=26;
}
//sum/剩下位数的可能 就是当前位应该填多少
str[j]=sum/tmpsum+'a';
//更新sum,以消除前面位数的影响
sum=sum%tmpsum;
}
for(int i=0;i<L;i++)
{
cout<<str[i];
}
return 0;
}