PTAL1-050 倒数第N个字符串解题报告---思维题

版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/85101463

                             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

总字符串数-倒数第N个字符串==顺数第几个字符串

AC Code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<cctype>
#include<map>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int main() {
	int N, L;
	while (scanf("%d%d", &L, &N) != EOF) {
		int v = (int)pow(26, L) - N;
		char res[10];
		for (int i = L - 1; i >= 0; i--) {
			res[i] = 'a' + v % 26;
			v /= 26;
		}
		for (int i = 0; i < L; i++) {
			printf("%c", res[i]);
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/85101463