Loading

java 蓝桥杯算法提高 _1区间k大数查询

 1 import java.util.Scanner;
 2 public class _1区间K大数查询 {
 3    public static void main(String[] args) {
 4       Scanner scanner = new Scanner(System.in);
 5       int n= scanner.nextInt();
 6       int[] shuzu = new int[n];
 7       for (int i = 0; i < shuzu.length; i++) {
 8         shuzu[i] = scanner.nextInt();
 9     }
10       int m = scanner.nextInt();
11       int output[] = new int[m];  
12       for (int i = 0; i < m; i++) {  
13           int l = scanner.nextInt();  
14           int r = scanner.nextInt();  
15           int k = scanner.nextInt();  
16           int[] K = new int[r - l + 1]; 
17           //将shuzu[j]l和r这部分赋给k
18           for (int j = l - 1; j < r; j++) {  
19               K[j + 1 - l] = shuzu[j];  
20           }  
21             //将这部分排序,从大到小排序了
22           for (int j = 0; j < K.length-1; j++) {  
23               for (int j2 = 0; j2 < K.length-1-j; j2++) {  
24                   if(K[j2]<K[j2+1]){  
25                       int temp = K[j2];  
26                       K[j2]= K[j2+1];  
27                       K[j2+1] = temp;  
28                   }  
29               }  
30           }  
31           output[i] = K[k-1];  
32       }  
33       for (int i = 0; i < m; i++) {  
34           System.out.println(output[i]);  
35       }  
36       
37       
38 }
39 }

 

posted @ 2017-03-22 15:09  爱笑的眼睛真美  阅读(340)  评论(0编辑  收藏  举报