题解 | #输入n个整数,输出其中最小的k个#
输入n个整数,输出其中最小的k个
https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
import java.util.Scanner;
import java.util.Arrays;
import java.util.Scanner;
import java.math.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x1 = input.nextInt();
if (Integer.toString(x1).length() < 1 || Integer.toString(x1).length() > 1000) {
System.out.println("X1 Error");
}
if (x1 < 1 || x1 > 1000) {
System.out.println("X1 Error");
}
int x2 = input.nextInt();
if (x2 < 1 || x2 > x1) {
System.out.println("X2 Error");
}
int[] arr = new int[x1];
for (int i = 0; i < x1; i++) {
arr[i] = input.nextInt();
if (arr[i] < 1 || arr[i] > 10000) {
System.out.println("X1 Error");
}
}
Arrays.sort(arr);
for (int i = 0; i < x2; i++) {
System.out.print(Integer.toString(arr[i]) + " ");
}
}
}