题解 | #截取字符串#
输入n个整数,输出其中最小的k个
http://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
// const line = readline();
function func() {
let input;
// #1. 第一个 readline() 获取第一行输入
while (input = readline()) { // 获取输入更方便 ‘5 2’
// input.split(' ') // ['5', '2']
let n = Number(input.split(' ')[0]) // 5
let k = Number(input.split(' ')[1]) // 2
// #2. 第二个 readline() 获取第二行输入 // '1 3 5 7'
// trim() 去除字符串的头尾空格
// readline().trim().split(' ') // ['1', '3', '5', '7']
let arr = readline().trim().split(' ').map(Number)
// 排序 sor()
arr.sort((a, b) => {
return a - b
});
const res = arr.splice(0 , k).join(' ')
print(res)
}
return ;
}
func();