题目描述:
找出n个数里最小的k个
输入描述:
每个测试输入包含空格分隔的n+1个整数,最后一个整数位k值,n不超过100
输出描述:
输出n个整数里最小的k个数。升序输出
示例描述:
输入:2 9 8 4 7 3 6 4
输出:2 3 4 6
代码实现:
public class Kmin {
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
String[] str;
str = reader.readLine().split(" ");
int n = str.length - 1;
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(str[i]);
}
Arrays.sort(arr);
int k = Integer.parseInt(str[str.length - 1]);
for (int i = 0; i < k - 1; i++) {
System.out.print(arr[i] + " ");
}
System.out.print(arr[k - 1]);
}
}