4

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

#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;
}
4

5 回答 5

11

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

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;
}
于 2013-10-06T14:54:20.477 回答
3

要收集所有答案而不仅仅是第一个答案,您可以使用std::vector<int> popular代替int popular.

那么当tempCount == count, popular.push_back(temp);,

tempCount > count,popular.clear(); popular.push_back(temp);

于 2013-10-06T14:50:42.533 回答
0

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

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;
   }
}
于 2016-12-20T16:11:19.040 回答
0

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

#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;
}
于 2018-01-31T15:22:10.157 回答
0

此类问题的完整功能:

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;
}
于 2018-11-12T20:28:58.887 回答