期中考试开始了,大家都想取得好成绩,争夺前五名。从键盘输入 n 个学生成绩,输出每组排在前五高的成绩。
数据范围: ,成绩采取百分制并不会出现负数
两行,第一行输入一个整数,表示n个学生(>=5),第二行输入n个学生成绩(整数表示,范围0~100),用空格分隔。
一行,输出成绩最高的前五个,用空格分隔。
6 99 45 78 67 72 88
99 88 78 72 67
5 10 20 30 40 50
50 40 30 20 10
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } bubble_sort(nums); for (int i = 0; i < 5; i++) { System.out.print(nums[i] + " "); } } //冒泡排序 public static void bubble_sort(int[] nums) { for (int i = 0; i < nums.length - 1; i++) { for (int j = 0; j < nums.length - 1; j++) { if (nums[j] < nums[j + 1]) { int tmp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = tmp; } } } } }
import java.util.Scanner; import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int a = Integer.parseInt(br.readLine()); String[] str = br.readLine().split(" "); ArrayList<Integer> score = new ArrayList<>(); // 因为 Collections.sort 默认按照字典顺序排序,需要将字符串转为整数 for(String item :str ) { score.add(Integer.parseInt(item)); } Collections.sort(score, Collections.reverseOrder()); score.stream().limit(5).forEach(x-> System.out.print(x + " ")); } }
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[] iList=new int[n]; for(int i=0;i<n;i++){ iList[i]=in.nextInt(); } Arrays.sort(iList); System.out.print(iList[n-1]+" "+iList[n-2]+" "+iList[n-3]+" "+iList[n-4]+" "+iList[n-5]); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int all=sc.nextInt(); int[] arr=new int[all]; for(int i=0;i<all;i++) arr[i]=sc.nextInt(); Arrays.sort(arr); for(int i=all-1;i>=all-5;i--) System.out.printf("%d ",arr[i]); } }
import java.util.*; public class Main { //方法:冒泡排序(从大到小顺序) public static int[] maopao(int[] arr) { int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] < arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } //主程序:输入数组,调用冒泡,输出 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int[] arr1 = maopao(arr); //循环输出前5个成绩,注意加” “ for (int i = 0; i < 5; i++) { System.out.print(arr1[i] + " "); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int toAddNumCount = scanner.nextInt(); int[] toNumArr = new int[toAddNumCount]; //输入数据 for(int i = 0;i < toNumArr.length;i++){ toNumArr[i] = scanner.nextInt(); } //使用冒泡排序法进行排序 int temp = toNumArr[0]; for(int j = 0;j < toNumArr.length;j++){ for(int k = 0;k < toNumArr.length - 1 - j;k++){ if(toNumArr[k] < toNumArr[k + 1]){ temp = toNumArr[k]; toNumArr[k] = toNumArr[k + 1]; toNumArr[k + 1] = temp; } } } //输出前五个数 for(int l = 0;l < 5;l++){ System.out.print(toNumArr[l] + " "); } } } }
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; import java.util.stream.Collectors; /** * @Title: 争夺前五名 * @Remark: 期中考试开始了,大家都想取得好成绩,争夺前五名。从键盘输入 n 个学生成绩,输出每组排在前五高的成绩。 * 数据范围: 5≤n≤50 ,成绩采取百分制并不会出现负数 * 输入描述: * 两行,第一行输入一个整数,表示n个学生(>=5),第二行输入n个学生成绩(整数表示,范围0~100),用空格分隔。 * 输出描述: * 一行,输出成绩最高的前五个,用空格分隔。 * @Author: ijunfu * @Version: 1.0.0 * @Date: 2022-03-18 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); Integer[] arr = new Integer[size]; for(int i=0; i<size; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr, (a, b) -> b - a); String str = Arrays.stream(arr) .limit(5) .map(x -> String.valueOf(x)) .collect(Collectors.joining(" ")); System.out.println(str); in.close(); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { int num; int[] grade=new int[20]; int i; int t; Scanner sc1=new Scanner(System.in); num=sc1.nextInt(); for(i=0;i<num;i++){ grade[i]=sc1.nextInt(); } for(i=0;i<num;i++){ for(int j=i;j<num-1;j++){ if(grade[i]<grade[j+1]){ t=grade[i]; grade[i]=grade[j+1]; grade[j+1]=t; } } } for(i=0;i<5;i++){ System.out.print(grade[i]+" "); } System.out.println(); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Integer count = Integer.parseInt(sc.next()); Integer [] studentGrade = new Integer[count]; for(int i = 0;i<count;i++){ studentGrade[i] = Integer.parseInt(sc.next()); } Main m = new Main(); m.sort(studentGrade); for(int i = 0;i<5;i++){ System.out.print(studentGrade[i]+" "); } } private void sort(Integer[] studentGrade) { for(int j = 1;j<studentGrade.length;j++) { for (int i = 0; i < studentGrade.length - j; i++) { if (studentGrade[i] <= studentGrade[i + 1]) { Integer temp = studentGrade[i]; studentGrade[i] = studentGrade[i + 1]; studentGrade[i + 1] = temp; } } } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> list = new ArrayList<>(n); for(int i = 0;i<n;i++){ list.add(sc.nextInt()); } Collections.sort(list); List<Integer> nlist = new ArrayList<>(n); for(int j = 0;j<n;j++){ nlist.add(list.get(n-1-j)); } for(int i = 0;i<5;i++){ System.out.print(nlist.get(i)+" "); } } }
import java.util.Scanner ; import java.util.Arrays ; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while( sc.hasNext()){ int[] array = new int[sc.nextInt()]; for(int i=0 ; i<array.length ; i++){ array[i] = sc.nextInt(); } Arrays.sort(array); // sort方法排序默认是从小到大排序 // 这里又新建了一个数组,把从小到大排序后的数组从后往前放入新建数组中,则得到一个从大到小排列的数组 // 这里也可以不新建数组,直接在原来数组的基础,把前半部分和后半部分交换也可以得到一个从大到小排列的数组 int[] re_array = new int[array.length]; for(int j=0;j<array.length;j++){ re_array[j] = array[array.length-1-j]; } for(int k = 0; k<5; k++){ System.out.print(re_array[k]+" "); } } } }
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); String s = scanner.nextLine(); List<Integer> list = new ArrayList<>(); int k = Integer.parseInt(s); for (int i = 0; i < k; i++) { int a = scanner.nextInt(); list.add(a); } Collections.sort(list); // 反转 Collections.reverse(list); for (int j = 0; j < 5; j++) { System.out.print(list.get(j)); System.out.print(" "); } } }
import java.util.*; public class Main{ public static void main (String[] args){ Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] grade = new int[n]; int temp; for(int i = 0; input.hasNext();i++){ grade[i] = input.nextInt(); } for(int i = 0; i < grade.length;i++){ for(int m = 0 ; m < grade.length - 1;m++){ if(grade[m] < grade[m+1]){ temp = grade[m]; grade[m] = grade[m+1]; grade[m+1] = temp; } } } for(int i = 0; i<=3;i++){ System.out.print(grade[i] + " "); } System.out.print(grade[4]); } }
import java.io.*; public class Main{ public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); String[] str = bf.readLine().split(" "); int[] numbers = new int[n]; for (int i = 0;i<str.length;i++){ numbers[i] = Integer.parseInt(str[i]); } // 用冒泡排序(以降序的方式) for (int t = 0;t < numbers.length-1;t++){ for (int j = 0;j<numbers.length-1-t;j++){ if (numbers[j] < numbers[j+1]){ int temp = numbers[j]; numbers[j] = numbers[j+1]; numbers[j+1] = temp; } } } // 取出前五个 for (int m = 0;m<numbers.length-1;m++){ if (m > 4){ return; } System.out.print(numbers[m]+" "); } } }
可以用工具类来对数组排序
import java.util.*; public class Main { public static void main(String [] args) { Scanner sc=new Scanner(System.in); while(sc.hasNextInt()) { int n=sc.nextInt(); int [] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } Arrays.sort(arr); for(int i=arr.length-1;i>arr.length-6;i--) { System.out.print(arr[i]+" "); } } } }