1093 大样本统计

题目描述:
我们对 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 == 256
1 <= sum(count) <= 10^9
计数表示的众数是唯一的
答案与真实值误差在 10^-5 以内就会被视为正确答案

方法1:
主要思路:解题链接汇总
(1)一遍遍历中获得最大值,最小值,众数,总的数字数量和总的数字数量和;
(2)然后再单独找出中位数,根据总的数量是奇数还是偶数,确定中位数;

class Solution {
public:
    vector<double> sampleStats(vector<int>& count) {
        int min_v=-1,max_v=0,counts=0,mode=0,mode_v=0;
        long long sum_all=0;
        for(int i=0;i<=255;++i){
            if(count[i]!=0){
                if(min_v==-1){//确定最小值
                    min_v=i;
                }
                max_v=i;//确定最大值
                counts+=count[i];//确定总的数字数量
                sum_all+=count[i]*i;//确定总的数字数量和
                if(count[i]>mode_v){//确定众数
                    mode_v=count[i];
                    mode=i;
                }
            }
        }
        double mid_v;//确定中位数
        if(counts&1){//奇数个数字
            int mid=counts/2+1;
            for(int i=0;i<=255;++i){
                if(mid-count[i]<=0){
                    mid=i;
                    break;
                }
                mid-=count[i];
            }
            mid_v=mid;//中位数
        }
        else{//偶数个数字
            int left=counts/2,right=0;
            for(int i=0;i<=255;++i){
                if(left-count[i]<=0){ 
                    if(left-count[i]<0){
                        right=i;
                        left=i;
                        break;
                    }
                    left=i;
                    ++i;
                    while(count[i]==0){
                        ++i;
                    }
                    right=i;
                    break;
                }
                left-=count[i];
            }
            mid_v=(left+right)/2.0;//中位数
        }
        return {1.0*min_v,1.0*max_v,1.0*sum_all/counts,mid_v,1.0*mode};
    }
};
上一篇:pat 乙级 1093 字符串A+B


下一篇:C++编写一个加减乘除开方乘方计算器