三个数组求中位数,以及中位数的中位数----java算法实现

时间:2022-03-03 13:14:50

求三个数组的中位数,以及中位数的中位数。

 

import java.util.Arrays;

public class median {
public static void main(String[] args) {

//m=3,n=3
long[] a = {1,2,6,4,5,9};
long[] b = {3,9,23,51,5};
long[] c = {13,234,1,54,32};


Arrays.sort(a);  //用来排序的方法
Arrays.sort(b);
Arrays.sort(c);

long aa = med(a);
long bb = med(b); 
long cc = med(c);


System.out.println(aa);
System.out.println(bb);
System.out.println(cc);

long[] d = {aa,bb,cc};
Arrays.sort(d);
long dd = med(d);

System.out.println(dd);

}

public static long med(long[] a)
{
long m = 0;
int len = a.length;
if(len%2==0)
{
m = (long) ((a[len/2] + a[len/2-1])/2.0);
}
else 
{
m = a[(len-1)/2];
}
return m;
}
}