The first small integer k

Description

Existing positive integer n, n≤10000, the requirement that the k-th smallest integer n positive integer (an integer equal size are counted only once), k≤1000, a positive integer less than 30,000.

Input

Comprising a plurality of sets of test data, the behavior data of the first test each n and k, the second line is the value of n is a positive integer, integers separated by spaces.

Output

Value of the k-th smallest integer; if no solution, then output "NO RESULT".

Sample Input

10 3

1 3 3 7 2 5 1 2 4 6

Sample Output

3

Solution: use an array of clever or, if it is not possible to see the first two, the specific code as follows:

import java.util.Scanner;
public class homework {
public static void Kmin(){
  int Kmin[]=new int[30001];
  int k=0;
  Scanner sc=new Scanner(System.in);
  System.out.println("input:");
  int n=sc.nextInt();
  int key=sc.nextInt();
  for(int i=0;i<n;i++){
   int temp=sc.nextInt();
   if(Kmin[temp]==0)
    Kmin[temp]=1;
  }
  for(int i=1;i<30001;i++){  //找到第K小的整数
   if(Kmin[i]!=0){
    k++;
    if(k==key){
     System.out.println(i);
    }
   }
  }
  
 }
 public static void main(String[] args){
 	Kmin();
  }
}

Screenshot result
Here Insert Picture Descriptionhodgepodge of the first three characteristics are the use-array, the array subscript represents the value of the element, the array is stored in an array of special meaning, the first chapter is stored in the number of times the number that appears, to achieve weight the effect of the number of times the second number that appears is stored, to find the highest number of purposes appearance, this one, too. Three have full use of the array is a contiguous space ordered this feature, so more with this feature to solve the problem, but remember not to use too much space is needed for the problem in which after all is a space for time practice, therefore road was good but memory loss.
Come on!

Published 54 original articles · won praise 8 · views 5320

Guess you like

Origin blog.csdn.net/qq_43411555/article/details/102881596