输入整型数组和排序标识,对其元素按照升序或降序进行排序
数据范围: ,元素大小满足
第一行输入数组元素个数第二行输入待排序的数组,每个数用空格隔开第三行输入一个整数0或1。0代表升序排序,1代表降序排序
输出排好序的数字
8 1 2 4 9 3 55 64 25 0
1 2 3 4 9 25 55 64
5 1 2 3 4 5 1
5 4 3 2 1
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 输入数组元素个数 int N = in.nextInt(); // 输入待排序的数组 int[] array = new int[N]; for (int i = 0; i < N; i++) { array[i] = in.nextInt(); } // 输入排序标识 int order = in.nextInt(); // 根据排序标识进行排序 if (order == 0) { Arrays.sort(array); } else if (order == 1) { Arrays.sort(array); // 反转数组实现降序 reverseArray(array); } // 输出排好序的数组 for (int value : array) { System.out.print(value + " "); } System.out.println(); in.close(); } // 反转数组 private static void reverseArray(int[] array) { int start = 0; int end = array.length - 1; while (start < end) { int temp = array[start]; array[start] = array[end]; array[end] = temp; start++; end--; } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(in.nextInt()); } final int sort = in.nextInt(); list.sort(Comparator.comparingInt(e->sort==0?e:-e)); list.forEach(e -> System.out.print(e + " ")); System.out.println(); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); List<Integer> r = new ArrayList(); for (int i = 0; i < n; ++ i) { r.add(in.nextInt()); } Collections.sort(r); if (in.nextInt() == 1) Collections.reverse(r); r.forEach(item -> { System.out.printf("%d ", item);}); } }
import java.util.*; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int numSize = in.nextInt(); List<Integer> nums = new ArrayList<>(); for (int i = 0; i < numSize; i++) { nums.add(in.nextInt()); } List<Integer> list = nums.stream().sorted().collect(Collectors.toList()); int lastInt = in.nextInt(); if (lastInt == 0) { for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } } else { for (int i = list.size() - 1; i >= 0; i--) { System.out.print(list.get(i) + " "); } } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int k = in.nextInt(); Integer[] nums = new Integer[k]; for (int i = 0; i < k; i++) { nums[i] = in.nextInt(); } if (in.nextInt() == 0) { Arrays.sort(nums); } else { Arrays.sort(nums, (a, b)->(b-a)); } for (int num : nums) { System.out.print(num + " "); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); List<Integer> list = new ArrayList(); for(int i = 0; i < n; i++){ list.add(in.nextInt()); } int t = in.nextInt(); Collections.sort(list, (o1, o2)-> t == 0 ? o1 - o2: o2 - o1); list.forEach(k -> System.out.print(k + " ")); } }
import java.util.ArrayList; import java.util.Comparator; import java.util.Iterator; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); ArrayList<Integer> list = new ArrayList<>(); String arrstr = sc.nextLine(); String[] split = arrstr.split(" "); for (int i = 0; i < split.length; i++) { int num = Integer.parseInt(split[i]); list.add(num); } String sss = sc.nextLine(); int i = Integer.parseInt(sss); if (i == 0) { sx(list); bl(list); } else { jx(list); bl(list); } } //升序方法 public static void sx(ArrayList<Integer> list) { list.sort(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1 - o2; } }); } //降序方法 public static void jx(ArrayList<Integer> list) { list.sort(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }); } //遍历list public static void bl(ArrayList<Integer> list) { Iterator<Integer> it = list.iterator(); while (it.hasNext()) { Integer next = it.next(); System.out.print(next + " "); } } }
import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.TreeSet; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int totalNum = sc.nextInt(); int[] source = new int[totalNum]; for (int i = 0; i < totalNum; i++) { source[i] = sc.nextInt(); } boolean orderFlag = 0 == sc.nextInt(); TreeSet<Integer> integers = new TreeSet<>((pre, current) -> { int diff = pre - current; diff = orderFlag ? diff : -diff; return diff == 0 ? 1 : diff; }); for (int i : source) { integers.add(i); } StringBuilder result = new StringBuilder(); for (Integer integer : integers) { result.append(" ").append(integer); } System.out.println(result.delete(0, 1)); } }
import java.util.Scanner; import java.util.Arrays; import java.util.Collections; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); int[] array = new int[a]; for (int i = 0; i < a; i++) { array[i] = in.nextInt(); } int b = in.nextInt(); if (b == 0) { Arrays.sort(array); for (int y = 0; y < a; y++) { System.out.print(array[y] + " "); } } else { for (int u = 0; u < a; u++) { for (int i = 0; i < a - 1; i++) { if (array[i] < array[i + 1]) { int num = array[i + 1]; array[i + 1] = array[i]; array[i] = num; } } } for (int y = 0; y < a; y++) { System.out.print(array[y] + " "); } } } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); Integer[] arr = new Integer[a]; for(int i = 0;i< a ;i++){ int c = in.nextInt(); arr[i] = c; } int b = in.nextInt(); if(b == 0){ Arrays.sort(arr); }else{ Arrays.sort(arr,Collections.reverseOrder()); } for(int i : arr){ System.out.print(i+" "); } } }
简单粗暴
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { int length = input.nextInt(); int[] datas = new int[length]; for (int i = 0; i < length; i++) { datas[i] = input.nextInt(); } int order = input.nextInt(); arrayOrder(length, order, datas); } } private static void arrayOrder(int length, int order, int[] datas) { List<Integer> arrayList = new ArrayList<>(length); for (int data : datas) { arrayList.add(data); } if (order == 0) { // 升序 Collections.sort(arrayList); } else { // 降序 arrayList.sort((element1, element2) -> element1 > element2 ? -1 : 1 ); } arrayList.forEach(element -> System.out.print(element + " ")); System.out.println(); } }