你们能帮我用哪些 apache-commons-math 类来计算前三分之一人口的平均值。
要计算我知道可以使用的平均值org.apache.commons.math3.stat.descriptive.DescriptiveStatistics
。
如何获得前三分之一的人口?
例子
人口:0、0、0、0、0、1、2、2、3、5、14
前三分之一:2、3、5、14
平均 = 24/4= 6.0
你们能帮我用哪些 apache-commons-math 类来计算前三分之一人口的平均值。
要计算我知道可以使用的平均值org.apache.commons.math3.stat.descriptive.DescriptiveStatistics
。
如何获得前三分之一的人口?
例子
人口:0、0、0、0、0、1、2、2、3、5、14
前三分之一:2、3、5、14
平均 = 24/4= 6.0
首先,您所说的前三分之一人口是什么?如果 set 除以 3 并且余数为 0,那么它很简单,但在你的情况下 11%3 = 2。所以你应该知道当余数不等于 0 时如何获得前三分之一。
我建议你使用 Arrays 程序,以获得集合的前三分之一。如果您仍想使用 DescriptiveStatistics,您可以调用它。
double[] set = {0, 0, 0, 0, 0, 1, 2, 2, 3,4,5};
Arrays.sort(set);
int from = 0;
if (set.length % 3==0){
from = set.length/3*2 ;
}
if (set.length % 3 != 0){
from = Math.round(set.length/3*2) + 1;
}
double [] topThirdSet = Arrays.copyOfRange(set,from , set.length);
DescriptiveStatistics ds = new DescriptiveStatistics(topThirdSet);
System.out.println(ds.getMean());