题解 | #输入n个整数,输出其中最小的k个#
输入n个整数,输出其中最小的k个
https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
def find_k_min_asc(n,k,numbers): rank_num = sorted(numbers) rank_first_k_num = rank_num[:k] return ' '.join(map(str,rank_first_k_num)) if __name__ == '__main__': n,k = map(int,input().split()) numbers = map(int,input().split()) assert 1<= n <= 1000 and k <= n print(find_k_min_asc(n,k,numbers))
先将numbers进行升序排序,再将前k个数取出,[:k],再者将k个数空格隔开
以主程序运行条件下,输入n,k = map(int,input().split()) numbers = map(int,input().split()) 先读取整行数据,在切分空格为数组如1 2 3 split操作后为[1,2,3],map后转化为整型
限定n的取值范围和k的取值范围