stream复习 | #输入n个整数,输出其中最小的k个#
输入n个整数,输出其中最小的k个
https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Integer> ts = new ArrayList<>(); int num = in.nextInt(), k = in.nextInt(); for(int i=0;i<num;i++) ts.add(in.nextInt()); ts.stream().sorted().limit(k).forEach(i -> System.out.print(i+" ")); } }
ts.stream().sorted().limit(k).forEach(i -> System.out.print(i+" "));