【问题标题】:Most occurring element in an array using c++?使用 C++ 的数组中出现次数最多的元素?
【发布时间】:2013-10-13 03:36:53
【问题描述】:

我尝试了以下代码来获取数组中出现次数最多的元素。它运行良好,但唯一的问题是当有两个或多个元素具有相同的出现次数并且等于大多数出现的元素时,它只显示扫描的第一个元素。请帮我解决这个问题。

#include <iostream>
using namespace std;
int main()
{
    int i,j,a[5];
    int popular = a[0];
    int temp=0, tempCount, count=1;
    cout << "Enter the elements: " << endl;
    for(i=0;i<5;i++)
        cin >> a[i];
    for (i=0;i<5;i++)
    {
        tempCount = 0;
        temp=a[i];
        tempCount++;
        for(j=i+1;j<5;j++)
        {
            if(a[j] == temp)
            {
                tempCount++;
                if(tempCount > count)
                {
                    popular = temp;
                    count = tempCount;
                }
            }
        }
    }
    cout << "Most occured element is: " <<  popular;
}

【问题讨论】:

  • 如果出现平局,是否要同时输出这两个数字?
  • 因为你是新手,所以我给你一个建议:避免using namespace std;。阅读this thread,了解为什么这是一种不好的做法。
  • @Lakshayarora:这对初学者来说是一个可怕的建议。这对他有什么帮助?
  • @khajvah 在循环声明之外声明循环计数器变量通常是个坏主意。
  • @g-makulik 好的,是的,在for循环的情况下,没有必要在外面声明。

标签: c++ arrays algorithm


【解决方案1】:

此类问题的完整函数:

int calcMode(int array[], int array_size)
{
int topCount=0, count, topElement = 10;

for ( int i=0 ; i<array_size ;i++)
{
    count=0;
    for (int j=0 ; j<array_size ; j++)
    {
        if (array[i]==array[j]) count++;
    }
    if (count>=topCount)
    {
        topCount=count;
        if (topElement > array[i])
            topElement=array[i];
    }
}

return topElement;
}

【讨论】:

    【解决方案2】:

    我认为这个解决方案会更好,而且更短。

    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    int topCount=0, count, topElement, array[10];
    
    for (int i=0 ; i<10 ; i++)
    {
        cin>>array[i];
    }
    
    for ( int i=0 ; i<10 ;i++)
    {
        count=0;
        for (int j=0 ; j<10 ; j++)
        {
            if (array[i]==array[j]) count++;
        }
        if (count>topCount)
        {
            topCount=count;
            topElement=array[i];
        }
    }
    
    cout<<topElement<<" "<<topCount;
    }
    

    【讨论】:

      【解决方案3】:

      这是一个模板化解决方案:

      template <class Iter, class ValType>
      void findMostCommon_ (Iter first, Iter last)
      {      
         typename std::vector<ValType> pop;
         int popular_cnt = 0;
      
         for (Iter it = first;   it != last;    ++it)
         {
            int temp_cnt = 0;
      
            for (Iter it2 = it + 1;  it2 != last;      ++it2)
               if (*it == *it2)
                  ++temp_cnt;
      
            if (temp_cnt)
            {
               if (temp_cnt > popular_cnt)
               {
                  popular_cnt = temp_cnt;
                  pop.clear();
                  pop.push_back(*it);
               }
               else if (temp_cnt == popular_cnt)
               {
                  pop.push_back(*it);
               }
            }
         }
      
         if (pop.empty())  // all numbers unique
         {
            cout << "Could not find most popular" << endl;
         }
         else`enter code here`
         {
            cout << "Most popular numbers: ";
      
            for (typename std::vector<ValType>::const_iterator it = pop.begin(), lst = pop.end();   it != lst;    ++it)
               cout << (*it) << " ";
            cout << endl;
         }
      }
      

      【讨论】:

      • 想详细说明这个答案吗?
      • 它将获得支持迭代器(包括 c 样式数组)的任何类型容器的最重复数字的列表:例如:
      【解决方案4】:

      重复两次解决方案并更改两行。

      if (count>max_count)
          max_count = count;
      

      与:

      if (count==max_count)
          cout << a[i] << endl;
      

      解决方案:

      int a[5];
      for (int i=0;i<5;i++)
         cin>>a[i];
      
      int max_count = 0;
      
      for (int i=0;i<5;i++)
      {
         int count=1;
         for (int j=i+1;j<5;j++)
             if (a[i]==a[j])
                 count++;
         if (count>max_count)
            max_count = count;
      }
      
      for (int i=0;i<5;i++)
      {
         int count=1;
         for (int j=i+1;j<5;j++)
             if (a[i]==a[j])
                 count++;
         if (count==max_count)
             cout << a[i] << endl;
      }
      

      【讨论】:

      • 感谢@hasan,这是该程序的完美解决方案 :)
      • 你能接受它作为你问题的答案并投票吗:)
      • 我试过但没有足够的声誉。我会在获得资格后投票。
      • 出现平局时如何让它选择两者中较大的一个?
      • @BreonThibodeaux 该解决方案会打印所有这些值。很容易,您可以修改解决方案以仅获得最大的数字。搜索数组的最大值。
      【解决方案5】:

      要收集所有答案而不仅仅是第一个答案, 您可以使用std::vector&lt;int&gt; popular 代替int popular

      那么当tempCount == countpopular.push_back(temp);

      tempCount &gt; countpopular.clear(); popular.push_back(temp);

      【讨论】:

        猜你喜欢
        • 2013-05-01
        • 2010-11-06
        • 2011-04-17
        • 2019-04-29
        • 2020-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多