题目大意:
给一串数字,求出中间数。
数字个数 1w 以内。
数字范围 [1,1000000]。
样例:
5
2
4
1
3
5
——
3
解题思路:
直接sort就可以了,
输出 [n/2] 即可。
AC代码:
1 import java.util.*; 2 3 public class Main{ 4 5 public static void main(String args[]){ 6 Scanner sc = new Scanner(System.in); 7 while(sc.hasNext()){ 8 int n = sc.nextInt(); 9 int m[] = new int[n]; 10 for(int i = 0;i < n;i ++){ 11 m[i] = sc.nextInt(); 12 } 13 Arrays.sort(m); 14 System.out.println(m[n / 2]); 15 } 16 } 17 18 }
本文介绍了一种求解数字序列中间数的简单算法。通过排序输入的数字序列,然后直接选择位于中间位置的数字作为结果。适用于数字个数在1万个以内,范围在[1,1000000]的场景。使用Java实现,包括读取输入、排序和输出中间数。
87

被折叠的 条评论
为什么被折叠?



