算法计算模式 [英] Algorithm to compute mode

查看:157
本文介绍了算法计算模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制定在接受两个参数,一个数组的数组的大小函数形式的算法。我希望它返回阵列的方式,如果有多种模式,返回它们的平均值。我的策略是采取阵列和第一个排序。再算上一些所有出现。而这一数字正在发生,添加一个计数器,商店,数出数组中米因此,m为持有所有的计数和另一个阵问:持有我们比较的最后一个值。

例如:我的名单 {1,1,1,1,2,2,2} 然后我会在 M [0] = 4 Q [0] = 1 然后米[1] = 3和q [1] = 2。

这样的模式为 Q [0] = 1;

不幸的是我没有成功迄今。希望有人能够帮助。

 浮充模式(INT X [],INT N)
{
    //数组复制和排序
    INT Y [N],温度,K = 0,计数器= 0,M [N],Q [N];

    的for(int i = 0;我n种;我++)
        值Y [i] = X [i]于;

    对于(INT通= 0;通过n种 -  1;通++)
        为(中间体POS = 0; POS n种; POS ++)
            如果(γ[通]≥Y [POS]){
                TEMP = Y [通]
                Y [通] = Y [POS]
                Y [POS] =气温;
            }

    对(INT I = 0; I&n种){
        为(诠释J = 0; J&n种; J ++){
            而(Y [I] == Y [J]){
                反++;
                我++;
            }
        }
        M [K] =计数器;
        Q [K] = Y [I]
        一世 - ; //我应该是1少,因为它指的是一个数组下标
        ķ++;
        计数器= 0;
    }

}
 

解决方案

即使你有一些很好的答案已经,我决定后另一个。我不知道它确实增加了很多这是新的,但我不能肯定它不会。如果不出意外,我是pretty的确保它使用多个标准头比任何其他的答案。 : - )

 的#include<载体>
#包括<算法>
#包括< unordered_map>
#包括<地图>
#包括<的iostream>
#包括<实用>
的#include<功能>
#包括<数字>

诠释的main(){
    的std ::矢量< INT>输入{1,1,1,1,2,2,2};

    的std :: unordered_map< INT,为size_t>数;
    对于(INT I:输入)
        ++计数[我]

    的std :: multimap中<为size_t,INT的std ::更大<为size_t> > INV;
    为(自动号码:计数)
        inv.insert(的std :: make_pair(p.second时,p.first));

    汽车E = inv.upper_bound(inv.begin() - >首先);

    双总和=的std ::累加(inv.begin()
        即
        0.0,
        [](双一,标准::对<为size_t,INT>常量和b){返回一个+ b.second; });

    性病::法院<<总之/性病::距离(inv.begin(),E);
}
 

相比@迪特马尔的回答,这应该是更快,如果你有很多重复的数字,但他可能会更快,如果该号码的大多的唯一性。

I'm trying to devise an algorithm in the form of a function that accepts two parameters, an array and the size of the array. I want it to return the mode of the array and if there are multiple modes, return their average. My strategy was to take the array and first sort it. Then count all the occurrences of a number. while that number is occurring, add one to counter and store that count in an array m. So m is holding all the counts and another array q is holding the last value we were comparing.

For example: is my list is {1, 1, 1, 1, 2, 2, 2} then i would have m[0] = 4 q[0] = 1 and then m[1] = 3 and q[1] = 2.

so the mode is q[0] = 1;

unfortunately i have had no success thus far. hoping someone could help.

float mode(int x[],int n)
{
    //Copy array and sort it
    int y[n], temp, k = 0, counter = 0, m[n], q[n];

    for(int i = 0; i < n; i++)
        y[i] = x[i];

    for(int pass = 0; pass < n - 1; pass++)
        for(int pos = 0; pos < n; pos++)
            if(y[pass] > y[pos]) {
                temp = y[pass];
                y[pass] = y[pos];
                y[pos] = temp;
            }

    for(int i = 0; i < n;){
        for(int j = 0; j < n; j++){
            while(y[i] == y[j]) {
                counter++;
                i++;
            }
        }
        m[k] = counter;
        q[k] = y[i];
        i--; //i should be 1 less since it is referring to an array subscript
        k++;
        counter = 0;
    }

}

解决方案

Even though you have some good answers already, I decided to post another. I'm not sure it really adds a lot that's new, but I'm not at all sure it doesn't either. If nothing else, I'm pretty sure it uses more standard headers than any of the other answers. :-)

#include <vector>
#include <algorithm>
#include <unordered_map>
#include <map>
#include <iostream>
#include <utility>
#include <functional>
#include <numeric>

int main() {
    std::vector<int> inputs{ 1, 1, 1, 1, 2, 2, 2 };

    std::unordered_map<int, size_t> counts;
    for (int i : inputs)
        ++counts[i];

    std::multimap<size_t, int, std::greater<size_t> > inv;
    for (auto p : counts)
        inv.insert(std::make_pair(p.second, p.first));

    auto e = inv.upper_bound(inv.begin()->first);

    double sum = std::accumulate(inv.begin(),
        e,
        0.0,
        [](double a, std::pair<size_t, int> const &b) {return a + b.second; });

    std::cout << sum / std::distance(inv.begin(), e);
}

Compared to @Dietmar's answer, this should be faster if you have a lot of repetition in the numbers, but his will probably be faster if the numbers are mostly unique.

这篇关于算法计算模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆