描述 | |
---|---|
知识点 | 循环,数组 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入说明 |
输出 | 输出一个整数数组 |
样例输入 | 5 2 1 3 5 7 2 |
样例输出 | 1 2 |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++)
nums[i] = sc.nextInt();
sc.close();
Arrays.sort(nums); //从小到大排序
for(int i = 0; i < k; i++){ //输出前k个数
if(i != k-1)
System.out.print(nums[i] + " ");
else
System.out.print(nums[i]); //最后一个数后面不加空格
}
}
}