禅城技术支持骏域网站建设,wap网站浏览器,苏州 网站的公司哪家好,wordpress保存的字体大小题目描述
我们对 0 到 255 之间的整数进行采样#xff0c;并将结果存储在数组 count 中#xff1a;count[k] 就是整数 k 的采样个数。
我们以 浮点数 数组的形式#xff0c;分别返回样本的最小值、最大值、平均值、中位数和众数。其中#xff0c;众数是保证唯一的。
我们…题目描述
我们对 0 到 255 之间的整数进行采样并将结果存储在数组 count 中count[k] 就是整数 k 的采样个数。
我们以 浮点数 数组的形式分别返回样本的最小值、最大值、平均值、中位数和众数。其中众数是保证唯一的。
我们先来回顾一下中位数的知识
如果样本中的元素有序并且元素数量为奇数时中位数为最中间的那个元素如果样本中的元素有序并且元素数量为偶数时中位数为中间的两个元素的平均值。
示例 1
输入count [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
输出[1.00000,3.00000,2.37500,2.50000,3.00000]
示例 2
输入count [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
输出[1.00000,4.00000,2.18182,2.00000,1.00000]
提示
count.length 2561 sum(count) 10^9计数表示的众数是唯一的答案与真实值误差在 10^-5 以内就会被视为正确答案
解法
直接求解即可 求和防溢出使用long类型
public double[] sampleStats(int[] count) {//最小值、最大值、平均值、中位数和众数int min Integer.MAX_VALUE, max Integer.MIN_VALUE, numCount0, zs0, countMax0;long valCount0;double[] res new double[5];if(count null || count.length1) {return res;}for(int i0;icount.length;i) {if(count[i]!0) {min Math.min(min, i);if(count[i] countMax) {zs i;}max Math.max(max, i);countMax Math.max(countMax, count[i]);numCount count[i];valCount i*count[i];}}if(numCount%2 0) {int tmp0, first0, second0;for(int i0;icount.length;i) {if(count[i]!0) {tmp count[i];if(tmpnumCount/2 first0) {first i;}if(tmpnumCount/21 second0) {secondi;}if(first!0 second!0) {res[3] (double)(firstsecond)/(double)2;break;}}}}else {int tmp0;for(int i0;icount.length;i) {if(count[i]!0) {tmp count[i];if(tmpnumCount/2) {res[3] i;break;}}}}res[0] min;res[1] max;res[2] (double)valCount/(double)numCount;res[4] zs;return res;}