题解 | #输入n个整数,输出其中最小的k个#
输入n个整数,输出其中最小的k个
https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int n = scanner.nextInt(); int k = scanner.nextInt(); ArrayList<Integer> integers = new ArrayList<>(); for (int i = 0; i <n; i++) { int i1 = scanner.nextInt(); integers.add(i1); } Collections.sort(integers); for (int i = 0; i <k; i++) { System.out.print(integers.get(i)+" "); } } } }