L1-2 倒数第N个字符串 (15分) java

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

输入格式:

输入在一行中给出两个正整数 L(2 ≤\le L ≤\le 6)和 N(≤105\le 10^5105)。

输出格式:

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

输入样例:

3 7417

输出样例:

pat
import java.io.IOException;
import java.io.*;
import java.util.*;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

public class Main {
	
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int l = Reader.nextInt();
		int n = Reader.nextInt();
		char[]ans = new char[l];

		int total = (int)(Math.pow(26, l));//总计结果数
		n = total-n;//正着数第几个
		int index=0;
		while (n>0) {
			int t = n%26;
			ans[index++] = (char)(t+'a');
			n /=26;
		}
		for (int i = 0; i < l-index; i++) {
			System.out.print('a');
		}
		for (int i = index-1; i >=0 ; i--) {
			System.out.print(ans[i]);
		}
			
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章