试题 算法训练 景点游览
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
小明来到一个景区游玩,但是他的时间有限,没办法逛遍所有的景点,所以他从网上下载了每个景点的评分,他希望能够使游览的景点的评分总和最高,于是他希望你帮他对于N个景点排下序。
输入格式
输入的第一行包含一个正整数N,表示N个景点。
第二行有N个正整数,表示每个景点的评分值。
输出格式
输出一行,包含N个正整数,表示N个景点的评分从大到小的排列
样例输入
4
3 2 4 1
样例输出
4 3 2 1
数据规模和约定
N<=1000,每个景点的评分<=10000。
Java 代码:
import java.io.*;
public class Main {
private static int[] a;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
a = new int[n];
String[] s = in.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(s[i]);
}
quickSort(0, n - 1);
for (int i = n - 1; i >= 0; i--) {
System.out.print(a[i] + " ");
}
}
private static void quickSort(int l, int r) {
if (l > r)
return;
int pivot = a[r];
int i = l;
int temp;
for (int j = i; j < r; j++) {
if (a[j] < pivot) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
}
}
temp = a[i];
a[i] = a[r];
a[r] = temp;
quickSort(l, i - 1);
quickSort(i + 1, r);
}
}