9.13用友 经验
9.13 用友笔试
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main3 {
public static String[] printAllPermutations1(int n) {
if (n >= 0 && n <= 9) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i <= n; i++) {
sb.append(i);
}
char[] chs = sb.toString().toCharArray();
ArrayList<String> list = new ArrayList<String>();
// ArrayList<String> lists = process1(list, chs, 0);
ArrayList<String> lists = process1(list, chs, 0);
String[] str = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
str[i] = list.get(i);
}
Arrays.sort(str);// 排序
// list.add(String.valueOf(list.size()));
//
// str[list.size()] = String.valueOf(list.size() - 1);
return str;
} else {
return null;
}
}
// 递归打印字符串的全排列
private static ArrayList<String> process1(ArrayList<String> list, char[] chs, int i) {
if (i == chs.length) {
// System.out.println(String.valueOf(chs));
list.add(String.valueOf(chs));
return list;
}
for (int j = i; j < chs.length; j++) {
swap(chs, i, j);
process1(list, chs, i + 1);
swap(chs, i, j);
}
return list;
}
// 将数组中的2个数交换顺序
private static void swap(char[] chs, int i, int j) {
char temp = chs[i];
chs[i] = chs[j];
chs[j] = temp;
}
// 测试
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] list = printAllPermutations1(n);
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
System.out.println(list.length);
}
}
#用友##笔试题目#import java.util.Arrays;
import java.util.Scanner;
public class Main3 {
public static String[] printAllPermutations1(int n) {
if (n >= 0 && n <= 9) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i <= n; i++) {
sb.append(i);
}
char[] chs = sb.toString().toCharArray();
ArrayList<String> list = new ArrayList<String>();
// ArrayList<String> lists = process1(list, chs, 0);
ArrayList<String> lists = process1(list, chs, 0);
String[] str = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
str[i] = list.get(i);
}
Arrays.sort(str);// 排序
// list.add(String.valueOf(list.size()));
//
// str[list.size()] = String.valueOf(list.size() - 1);
return str;
} else {
return null;
}
}
// 递归打印字符串的全排列
private static ArrayList<String> process1(ArrayList<String> list, char[] chs, int i) {
if (i == chs.length) {
// System.out.println(String.valueOf(chs));
list.add(String.valueOf(chs));
return list;
}
for (int j = i; j < chs.length; j++) {
swap(chs, i, j);
process1(list, chs, i + 1);
swap(chs, i, j);
}
return list;
}
// 将数组中的2个数交换顺序
private static void swap(char[] chs, int i, int j) {
char temp = chs[i];
chs[i] = chs[j];
chs[j] = temp;
}
// 测试
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] list = printAllPermutations1(n);
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
System.out.println(list.length);
}
}
时间没来及,之前提交的时候是25%,不知道改完之后是多少