从题目上看,用int类型数据就够了,没必要考虑long这些~~ 在题目中,直接用了Collection工具类中的sort方法,是从小到大排序,据了解,java自带的排序算法应该是优化过的快速排序,算法可靠。
package 区间k大数查询;import java.util.ArrayList;import java.util.Collections;import java.util.Scanner;public class Main { public static void main(String[] args){ int n, m, l, r, k; Scanner sc = new Scanner(System.in); n = sc.nextInt(); int a[] = new int[n]; for (int i=0;i<n;i++){ a[i] = sc.nextInt(); } m = sc.nextInt(); for (int i=0;i<m;i++){ l = sc.nextInt(); r = sc.nextInt(); k = sc.nextInt(); ArrayList<Integer> list = new ArrayList<Integer>(); for (int j=l-1;j<r;j++){ list.add(a[j]); //System.out.PRintln("add:"+a[j]); } Collections.sort(list);// for (int mm=0;mm<list.size();mm++){// System.out.println("list "+mm+"="+list.get(mm));// }// System.out.println("final:"+list.get(list.size()-k)); System.out.println(list.get(list.size()-k)); } }}新闻热点
疑难解答