【问题标题】:Finding Max, Min and Mode in c++在 C++ 中查找最大值、最小值和众数
【发布时间】:2018-03-15 02:24:44
【问题描述】:

所以基本上我正在尝试编写一个程序,它接受一个整数数组,然后输出 Max Min 和 smallest 模式以及它出现的次数。 我已经能够找到最大值和最小值以及模式,但是我的代码输出的不是最小模式,而是首先出现的模式。而且我不确定如何处理具有多种模式的输入。 下面我将发布我的代码: #包括 使用命名空间标准;

   int main() {
  int p,max,min,mode;


    cout << "Enter the number of postive integers:"<< endl;
    cin >> p;
    int pos[p];
    cout << "Now enter postive integers!" <<endl;
    for(int i=0; i<p; i++) {
        cout << "Positive integer " << i+1 << ":";
        cin >> pos[i]; }
     max =pos[0];
     min =pos[0];
    for( int i=0; i<p; i++){
        if(pos[i]> max) max=pos[i];
        if(pos[i]< min) min=pos[i];
    }
    cout << "Max=" << max << endl;
    cout << "Min=" << min <<     mode= pos[0];
    int count[20];
    int t=0;
        for(int c=0;c<p; c++)
        {
            for(int d=0;d<p;d++)
            {
                if(pos[c]==pos[d])
                {
                    count[c]++;
                    t++;
                }
            }

    int modepos, maxno=count[0];
            for(int e=1;e<p;e++)
            { 
                if(maxno<count[e])
                {
                    maxno=count[e];
                    modepos=e;
    }
    }
    mode=pos[modepos];
            if(t==1) {
                cout << "There is no positive integer occuring more       
    than once." << endl;

            }
            else {

            cout <<"The most occuring positive integer is:"<< mode;
            cout << "\nIt occurs " << t << " times." << endl;

            } return 0; }

可能有更简单更好的方法来编写代码,但是由于我是初学者并且只学习了循环/条件/数组/变量声明等,我只能在程序中使用它们,任何帮助将不胜感激。

【问题讨论】:

  • 对于模式,您可以使用地图 并在每次找到第二个值时增加第二个值,然后扫描一次以获取您刚刚比较和存储的最大值...最小值和最大值跨度>
  • int pos[p]; -- 这不是有效的 C++。 C++ 中的数组必须使用编译时常量表达式声明其大小,而不是变量。您说您是初学者,如果您想编写有效的 C++ 代码,请深入了解std::vector&lt;int&gt; pos(p); 的世界。 (再一次,gccclang 通过默认允许这种非标准语法导致另一个初学者程序员走上了错误的道路。)
  • 附注一二:请使用更长、更好的标识符名称,并采用常规缩进样式。两者都会帮助你理解自己的代码,更重要的是,也会帮助我们。
  • 如果您自己调试代码并弄清楚发生了什么,它将为您提供最好的服务。了解如何使用调试器。您可能不想在这一分钟停止您正在做的所有事情并掌握调试器,所以使用cout 作为您的临时替身。在每个 for()if()elsecout 变量的顶部,然后在运行代码时观察控制台,并查看程序的行为。
  • 最简单的方法就是对其进行排序。然后 min = 数组 [0]。 max = array[size - 1] 然后遍历排序后的数组并计算每个相同数字有多少个并保持总数运行,然后比较一个最小的次数。

标签: c++ arrays for-loop


【解决方案1】:

你了解 std::map 吗?使用 std::map 计算数组中元素的次数的算法非常简单

std::map<long, long > mapFreq;// first: for store value of array pos, second for store value's counter
mapFreq.insert(std::pair<long, long>(pos[0], 1));
for(int i = 1; i < dsize; i++)
{
    auto &it = mapFreq.find(pos[i]);
    if(it != mapFreq.end())
    {
        it->second++;
    }
    else
    {
        mapFreq.insert(std::pair<long, long>(pos[i], 1));
    }
}

然后你可以循环遍历 map Freq 来满足你的需要:

int number, counter;
for(auto it : mapFreq)
{
    if(it.second < counter)
    {
        number = it.first;
        counter = it.second;
    }
}

【讨论】:

  • 实际上我还没有学过,所以我不知道你的代码在做什么,有没有办法只使用 for 循环和所有?
  • 如果你只使用数组,你可以参考这个链接:stackoverflow.com/questions/25143224/…
  • 您甚至可以简化为:for(int i = 0; i &lt; dsize; i++) { ++mapFreq[pos[i]];}
【解决方案2】:

也许你可以尝试这样做:

#include <iostream>
#include <algorithm> //for sorting
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[n];
sort(a,a+n);
int b[a[n-1]];
for(int i=0;i<a[n-1];i++) b[i]=0;
for(int i=0;i<n;i++) b[a[i]]++;
cout<<"Largest number = "<<a[n-1]<<endl;
cout<<"Smallest number = "<<a[0]<<endl;
int rep=0;//repetition
int mode=0;
for (int i=0;i<a[n-1];i++){
  if(b[i]>rep){
    rep=b[i];// set times of repetition
    mode=i;// set new mode
  }
}
cout<<"Mode = "<<mode<<endl;
}

【讨论】:

  • 恭喜您在 Stackoverflow 上发布您的第一个答案。您当前的答案是纯代码答案。还请在您的回答中解释您的代码示例如何工作以及如何回答问题。请参阅此处了解为什么纯代码答案不是最佳答案类型的众多讨论之一meta.stackoverflow.com/questions/300837/…
猜你喜欢
  • 2015-12-24
  • 2017-08-18
  • 2018-05-03
  • 1970-01-01
  • 2012-09-16
  • 2013-11-12
  • 2017-04-20
  • 2021-09-15
相关资源
最近更新 更多