本文共 961 字,大约阅读时间需要 3 分钟。
向下取整?
Math.floor()
向上取整:ceil 向下取整:floor 四舍五入:round
第一遍:
import java.util.Arrays;import java.util.Scanner;public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(sc.hasNext()&&n!=0) { int a[]=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=sc.nextInt(); } Arrays.sort(a); if(n%2==0) { int result=(int) Math.floor((a[n/2]+a[(n/2)+1])/2); System.out.println(result); } if(n%2!=0) { int f=(int) Math.ceil(n/2); int result=a[f]; System.out.println(result); } } }}
奇怪?问题出在哪里呢?
后来把 new int[n+1]改成 new int[n]
让数组从0开始计数而不是从1开始,就又好了
import java.util.Arrays;import java.util.Scanner;public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); while(sc.hasNext()) { int n=sc.nextInt(); if(n==0) break; int a[]=new int[n]; for(int i=0;i
还是有一丝丝疑惑的……
转载地址:http://ecssi.baihongyu.com/