问题描述
数据很多,但我们经常只取前几名,比如奥运只取前3名。现在我们有n个数据,请按从大到小的顺序,输出前10个名数据。
输入格式
两行。
第一行一个整数n,表示要对多少个数据
第二行有n个整数,中间用空格分隔。表示n个数据。
输出格式
一行,按从大到小排列的前10个数据,每个数据之间用一个空格隔开。
样例输入
26
54 27 87 16 63 40 40 22 61 6 57 70 0 42 11 50 13 5 56 7 8 86 56 91 68 59
样例输出
91 87 86 70 68 63 61 59 57 56
数据规模和约定
10<=n<=200,各个整数不超出整型范围
资源限制
时间限制:1.0s 内存限制:256.0MB
代码块:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
//接收用户输入的数据
int n = sc.nextInt();
int[]a = new int[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextInt();
}
//使用冒泡排序的方法对数组进行排序
for(int i = 0; i < n-1; i++){
for(int j = n-1; j > 0; j--){
if(a[j] > a[j-1]){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
//打印排序后的数组的前十项
for(int i = 0; i < 10; i++){
System.out.print(a[i]+" ");
}
}
}