给定一个完全由小写英文字母组成的字符串等差递增序列,该序列中的每个字符串的长度固定为 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
解答:自己实现字母递减就好
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int L = sc.nextInt();
int findIndex = sc.nextInt();
StringBuilder builder = new StringBuilder();
for(int i=0; i<L; i++) {
builder.append("z");
}
String abcArray = builder.toString();
for(int i=1; i<findIndex; i++) {
abcArray = subABC(abcArray);
}
System.out.println(abcArray);
sc.close();
}
public static String subABC(String abcArray) {
StringBuilder sb =new StringBuilder(abcArray);
int end = sb.length()-1;
while(true) {
if(sb.charAt(end)!='a') {
sb.setCharAt(end, (char)(sb.charAt(end)-1));
break;
}else {
sb.setCharAt(end, 'z');
}
end--;
}
return sb.toString();
}
}