博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
中位数
阅读量:4107 次
发布时间:2019-05-25

本文共 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/

你可能感兴趣的文章
注册表修改DOS的编码页为utf-8
查看>>
matplotlib.pyplot.plot()参数详解
查看>>
拉格朗日对偶问题详解
查看>>
MFC矩阵运算
查看>>
最小二乘法拟合:原理,python源码,C++源码
查看>>
ubuntu 安装mysql
查看>>
c# 计算器
查看>>
C# 简单的矩阵运算
查看>>
gcc 常用选项详解
查看>>
c++输入文件流ifstream用法详解
查看>>
c++输出文件流ofstream用法详解
查看>>
字符编码:ASCII,Unicode 和 UTF-8
查看>>
QT跨MinGW和MSVC两种编译器的解决办法
查看>>
firewalld的基本使用
查看>>
Linux下SVN客户端使用教程
查看>>
i2c-tools
查看>>
Linux分区方案
查看>>
nc 命令详解
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>