1
蓝桥杯 2014 年以前 java 历年真题及答案整理
1、字符排序
如果给定 N 个不同字符,将这 N 个字符全排列,最终的结果将会是 N!种。如:给定 A、B、C 三个不同的字符,
则结果为:ABC、ACB、BAC、BCA、CAB、CBA 一共 3!=3*2=6 种情况。
public class Question1 {
public static long count=0;
private void fullPermutation(Vector<Character>sourse, Vector<Character> result) {
if(sourse.size()==0){
for (int i = 0; i < result.size(); i++) {
System.out.print(result.elementAt(i));
}
System.out.print("");
count++;
return;
}
for (int i = 0; i < sourse.size(); i++) {
Vector<Character>tsourse=new Vector<Character>(sourse);
Vector<Character>tresult=new Vector<Character>(result);
tresult.add(sourse.elementAt(i));
tsourse.remove(i);
new Question1().fullPermutation(tsourse, tresult);
}
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
Vector<Character> sourse=new Vector<Character>();
Vector<Character> result=new Vector<Character>();
for (int i = 0; i < n; i++) {