PTAL1-050 倒數第N個字串解題報告---思維題
阿新 • • 發佈:2019-01-13
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(≤105)。
輸出格式:
在一行中輸出對應序列倒數第 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; }