三個數組求中位數,以及中位數的中位數----java算法實現

求三個數組的中位數,以及中位數的中位數。java

 

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;
}
}
相關文章
相關標籤/搜索