【发布时间】:2017-08-17 16:54:07
【问题描述】:
如何编辑此功能以查找多种模式?现在如果有多个,它将显示最小的。
例子
输入 5 5 2 2 输出 5 2
它实际上做了什么
输入 5 5 1 1 输出 1
void calculateMode(int array[], int big)
{
int counter = 1;
int max = 0;
int mode = array[0];
for (int pass = 0; pass < big - 1; pass++)
{
if ( array[pass] == array[pass+1] )
{
counter++;
if ( counter > max )
{
max = counter;
mode = array[pass];
}
} else
counter = 1; // reset counter.
}
cout << "The mode is: " << mode << endl;
}
任何帮助!
【问题讨论】:
-
如果赋值允许使用标准库,考虑使用
std::map<int,int> freq;创建一个整数到你看到它的次数的映射:freq[array[pass]]++然后查看freq为最高计数。如果没有,那么您最好的选择是学习如何使用几乎可以肯定随您的开发环境附带的调试软件。一旦您开始逐行执行代码,您很快就会发现自己的错误。 -
Algorithm to compute mode 的可能重复项 ... Dietmar Kühl 的回答与您在这里尝试做的非常接近。
标签: c++ math computer-science mode