思路和一个水王一样,每次删除四个不同的ID。
值得注意的地方:设置candidate1、candidate2、candidata3存储ID,这三个变量存储的ID不能相同。
#include<iostream>
using namespace std;
void find(int * ID,int N)
{
int candidate1;
int candidate2;
int candidate3;
int nTimes1=0;
int nTimes2=0;
int nTimes3=0;
for(int i=0;i<N;i++)
{
if(nTimes1==0)
{
if(candidate2==ID[i])//candidate1不能等于candidate2,candidate3
{
nTimes2++;
}
else if(ID[i]==candidate3)
nTimes3++;
else
{
candidate1=ID[i];
nTimes1=1;
}
}
else if(nTimes2==0)
{
if(candidate1==ID[i])//candidate2不能等于candidate1,candidate3
{
nTimes1++;
}
else if(ID[i]==candidate3)
nTimes3++;
else
{
candidate2=ID[i];
nTimes2=1;
}
}
else if(nTimes3==0)
{
if(ID[i]==candidate1)//candidate3不能等于candidate2,candidate1
nTimes1++;
else if(ID[i]==candidate2)
nTimes2++;
else
{
candidate3=ID[i];
nTimes3=1;
}
}
else
{
if(candidate1==ID[i])
nTimes1++;
else if(candidate2==ID[i])
nTimes2++;
else if(candidate3==ID[i])
nTimes3++;
else
{
nTimes1--;
nTimes2--;
nTimes3--;
}
}
}
cout<<candidate1<<"--"<<candidate2<<"--"<<candidate3<<endl;
}
int main()
{
int a[]={1,1,3,2,2,3,1,2,3,4};
find(a,10);
int b[]={1,1,1,2,2,3,3,2,3,4};
find(b,10);
return 0;
}
智能算法在数据处理中的应用

1127

被折叠的 条评论
为什么被折叠?



