HJ58 输入n个整数,输出其中最小的k个

思路:

step1:首先输入n,k这两个参数(因为input()后是字符串类型,所以需要将n,k转换为int类型,这时我们需要使用list(map())函数);
step2:其次,输入一个整数数组num(原理同上,也需要转换为int型,同样需要list(map())函数;)
step3:使用sorted()函数,对num整数数组进行排序;
step4:使用for循环,遍历“排序后的、且使用切片切割好的”整数数组num;
step5:逐个打印,并使用空格分开

代码如下:

n,k = list(map(int,input().split()))
num = list(map(int,input().split()))
num = sorted(num)
for i in num[:k]:
    print(i,end=' ')