蓝桥杯- 基础练习:数列排序
Posted 懒蛋。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥杯- 基础练习:数列排序相关的知识,希望对你有一定的参考价值。
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int cNumber = scanner.nextInt(); List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < cNumber; i++) { list.add(scanner.nextInt()); } Collections.sort(list); String replace = list.toString().replaceAll("[\\\\[\\\\],]",""); System.out.println(replace); } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { //数组排序 Scanner scanner = new Scanner(System.in); int cNumber = scanner.nextInt(); int nums[] = new int[cNumber]; for (int i = 0; i < cNumber; i++) { nums[i]=scanner.nextInt(); } Arrays.sort(nums); for (int i = 0; i < nums.length; i++) { System.out.print(nums[i]+" "); } } }
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int cNumber = scanner.nextInt(); List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < cNumber; i++) { list.add(scanner.nextInt()); } list.sort(new Comparator<Integer>() { @Override public int compare(Integer arg0, Integer arg1) { return Integer.compare(arg0, arg1); } }); String replace = list.toString().replace("[", ""); String replace2 = replace.replace("]", ""); String replace3 = replace2.replace(",", ""); System.out.println(replace3); } }
以上是关于蓝桥杯- 基础练习:数列排序的主要内容,如果未能解决你的问题,请参考以下文章