使用c ++在数组中出现的大多数元素?

时间:2013-10-06 14:36:30

标签: c++ arrays algorithm

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

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

5 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:3)

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

然后是tempCount == countpopular.push_back(temp);

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

答案 2 :(得分: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;
   }
}

答案 3 :(得分: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;
}

答案 4 :(得分: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;
}