你们可以帮我解决一下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
答案 0 :(得分:1)
首先,你称之为人口的前三分之一? 如果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());