C++ 使用c++;?
我尝试了以下代码来获取数组中出现最多的元素。它工作得很好,但唯一的问题是,当有两个或多个元素具有相同的出现次数并等于大多数出现的元素时,它只显示扫描的第一个元素。请帮我解决这个问题C++ 使用c++;?,c++,arrays,algorithm,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 << "En
#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;
}
#包括
使用名称空间std;
int main()
{
int i,j,a[5];
int popular=a[0];
int temp=0,tempCount,count=1;
无法收集所有答案,而不仅仅是第一个答案,
您可以使用std::vector popular
而不是int popular
然后当tempCount==count
时,流行。向后推(temp);
当tempCount>count
时,popular.clear();popular.push_back(temp);
重复求解两次并换两行
if (count>max_count)
max_count = count;
与:
if(计数==最大计数)
cout这是一个模板化解决方案:
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;
}
}
模板
void findmostconomon_(Iter优先,Iter最后)
{
typename std::vector pop;
int popular_cnt=0;
for(Iter it=第一;it!=最后;++it)
{
内部温度=0;
对于(Iter it2=it+1;it2!=last;+it2)
如果(*it==*it2)
++温度;
如果(温度)
{
如果(临时>流行)
{
流行的=临时的;
pop.clear();
pop.推回(*它);
}
else if(临时控制==流行控制)
{
pop.推回(*它);
}
}
}
if(pop.empty())//所有数字都是唯一的
{
我认为这个解决方案会更好,而且更短
#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;
}
#包括
使用名称空间std;
int main()
{
int-topCount=0,count,topElement,数组[10];
对于(int i=0;i>array[i];
}
对于(int i=0;i此类问题的完整函数:
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;
}
int-calcMode(int-array[],int-array\u-size)
{
int topCount=0,count,topElement=10;
for(int i=0;i数组[i])
topElement=数组[i];
}
}
返回元素;
}
你想在平局时同时输出两个数字吗?因为你是新手,让我给你一个建议:避免使用名称空间std;
。阅读以了解为什么这是一个糟糕的做法。@Lakshayarora:这对初学者来说是一个可怕的建议。这对他有什么帮助?@khajvah在循环decl之外声明循环计数器变量aration通常是个坏主意。@g-makulik好的,是的,如果for
循环,没有必要在外面声明。谢谢@hasan这是这个程序的完美解决方案:)你能接受它作为你问题的答案并向上投票吗我试过了,但是没有足够的声誉。我会在获得资格后投票。当平局发生时,你怎么能让它选择两者中较大的一个呢?@Breonhibodeaux解决方案打印所有这些值。很容易你可以修改解决方案,只获得最大值。serach代表数组中的最大值。是否要详细说明这个答案?它会得到支持迭代器(包括c样式数组)的任何类型容器的重复次数最多的列表:Ex:
#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;
}
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;
}