输入n个整数,找出其中最小的k个整数并按升序输出
本题有多组输入样例
数据范围: ,输入的整数满足
import java.util.Scanner; import java.util.ArrayList; import java.util.List; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); int k = in.nextInt(); in.nextLine(); List<Integer> list = new ArrayList<>(); for (int i = 0; i < num; i++) { list.add(in.nextInt()); } list.sort(Integer::compareTo); for (int i = 0; i < k; i++) { System.out.print(list.get(i) + " "); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int [] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = in.nextInt(); } Arrays.sort(arr); for(int i = 0; i < k; i++) System.out.print(arr[i]+" "); } }
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringJoiner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); String line2 = br.readLine(); int n = Integer.parseInt(line.split(" ")[0]); int k = Integer.parseInt(line.split(" ")[1]); int[] numbers = new int[n]; String[] split = line2.split(" "); for (int i = 0; i < split.length; i++) { int num = Integer.parseInt(split[i]); numbers[i] = num; } // 数组排序 Arrays.sort(numbers); // 输出前k个 StringJoiner sj = new StringJoiner(" ", "", ""); for (int i = 0; i < Math.min(n, k); i++) { sj.add(String.valueOf(numbers[i])); } System.out.println(sj); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int n=in.nextInt(); int k=in.nextInt(); ArrayList<Integer> list=new ArrayList<>(); for(int i=0;i<n;i++){ list.add(in.nextInt()); } Collections.sort(list); for(int i=0;i<k;i++){ System.out.print(list.get(i)+" "); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); int k = in.nextInt(); int[] arr = new int[a]; for (int i = 0; i < a; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr); for (int j = 0; j < k; j++) { System.out.print(arr[j] + " "); } } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int numCount = in.nextInt(); int outputCount = in.nextInt(); List<Integer> inputList = new ArrayList<>(numCount); for (int i = 0; i < numCount; i++) { inputList.add(in.nextInt()); } Collections.sort(inputList); for (int i = 0; i < outputCount; i++) { System.out.print(inputList.get(i) + " "); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String l1 = br.readLine(); String l2 = br.readLine(); String[] s1 = l1.split(" "); String[] s2 = l2.split(" "); int n = Integer.parseInt(s1[0]); int k = Integer.parseInt(s1[1]); ArrayList<Integer> arr = new ArrayList<Integer>(n); int e; for(int i = 0; i < n; i++){ e = Integer.parseInt(s2[i]); arr.add(e); } Collections.sort(arr); for(int i = 0; i < k; i++){ System.out.printf( "%d ", arr.get(i)); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); String[] strs = in.nextLine().split(" "); int n = Integer.parseInt(strs[0]); int m = Integer.parseInt(strs[1]); PriorityQueue<Integer> queue = new PriorityQueue(); String[] str = in.nextLine().split(" "); for(int i=0; i<str.length; i++){ queue.add(Integer.parseInt(str[i])); } for(int i=0; i<m; i++){ System.out.print(queue.poll() + " "); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int k=sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } Arrays.sort(arr);//懒得手写排序了 for(int i=0;i<k-1;i++){ System.out.print(arr[i]+" "); } System.out.println(arr[k-1]);//最后一位输出记得换行 } } }