第三题
public class test8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
List<List<Integer>> list = new ArrayList<>();
backtrack(list, new ArrayList<>(), array);
for (List<Integer> list2 : list) {
for (Integer integer: list2) {
System.out.print(integer);
}
System.out.println();
}
}
public static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums){
if (templist.size() == nums.length) {
list.add(new ArrayList<>(templist));// 符合条件的加入进去
}
else {
for (int i = 0; i < nums.length; i++) {
if (templist.contains(nums[i])){ // 这个判断是否包含重复 也是及其精妙啊
continue;
}
templist.add(nums[i]);
backtrack(list, templist, nums);
templist.remove(templist.size() - 1);
}
}
}
}
第一题
public class test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
Set<String> set = new HashSet<>();
char[] chars = string.toCharArray();
int l = 0;
int r = 0;
for (int i = 0; i < string.length(); i++) {
while (chars[r] < '0' || chars[r] > '9') {
l++;
r++;
}
StringBuilder stringBuilder = new StringBuilder();
while (chars[r] >= '0' && chars[r] <= '9') {
stringBuilder.append(chars[r++]);
if (r >= string.length()) {
break;
}
}
set.add(stringBuilder.toString());
i = r - 1;
}
int res = Integer.MIN_VALUE;
for (String s : set) {
res = Math.max(res, Integer.valueOf(s));
}
System.out.println(res);
}
}
第二题 ,有点bug, 只通过了 16%, 没时间调了,
public class test7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
if (array.length == 1) {
System.out.println(array[0]);
}
else if (array.length == 2) {
System.out.println(array[0] + "," + array[1]);
}
else {
StringBuilder stringBuilder = new StringBuilder();
for (int i =0; i < array.length; i++) {
StringBuilder temp = new StringBuilder();
int start = array[i];
temp.append(start + "");
if ((start + 1) < array.length && array[start + 1] - array[start] > 1) {
stringBuilder.append(array[start] + ",");
}
else {
int temprr = Backing(temp, array, start + 1);
if (temprr >= 3) {
String ssss = array[start] + "-" + array[start + temprr - 1] + ",";
stringBuilder.append(ssss);
}
else {
stringBuilder.append(array[start] + ",");
}
}
}
System.out.println(stringBuilder.toString());
}
}
public static int Backing(StringBuilder stringBuilder, int[] array, int start) {
stringBuilder.append(array[start] + "");
if ((start + 1) < array.length && array[start + 1] - array[start] > 1) {
return stringBuilder.length();
}
return Backing(stringBuilder, array, start + 1);
}
}