我想使一个程序,将计数有多少号码已经重复例如计数在C++阵列
ARR1 [] = {1,2,3,4,5,1,2重复数}输出应该是2,因为数字1和2有重复
arr2 [] = {1,1,1,2,2,3,3,3}输出应该是3,因为数字1,2和3有重复
我不能看到具体回答我的问题,因为答案总是出现与计数多少次呢阵列中的重复次数
反正我的继承人自己的代码
#include <iostream>
using namespace std;
int main()
{
int arr[64],n,clamp=0,ctr=0,maxv = 0;
cout<<"Enter desired number of element"<<endl;
cin>>n;
cout<<"Enter "<<n<<" numbers"<<endl;
for(int x=0;x<n;x++)
{
cin>>arr[x];
}
for(int i=0;i<n;i++)
{
if(arr[i]>maxv)
maxv=arr[i];
}
for(int y=0;y<=maxv;y++)
{
for(int x=0;x<n;x++)
{
if(y==arr[x])
{
ctr++;
}
if(ctr>=2)
{
clamp++;
ctr=0;
break;
}
}
ctr=0;
}
cout<<"The amount of repeated elements is "<<clamp<<endl;
return 0;
}
是排序,然后接受检查能够? –
(你也可以使用散列表) –
欢迎来到Stack Overflow。请花些时间阅读[The Tour](http://stackoverflow.com/tour),并参阅[帮助中心](http://stackoverflow.com/help/asking)中的资料,了解您可以在这里问。 –