与之前同意的思路,只不过现在需设置3个candidate变量。若与此3个candidate的id均不相同,则count-1。
#include<iostream>
using namespace std;
const int N=16;
class Type{
public:
Type(){count=0;id=-1;}
int count;
int id;
};
int main(){
int ID[N]={1,2,3,4,5,3,2,1,1,1,2,2,3,3,4,4};
Type candidate1;
Type candidate2;
Type candidate3;
int i;
for(i=0;i<N;i++){
if(ID[i]==candidate1.id)
candidate1.count++;
else if(ID[i]==candidate2.id)
candidate2.count++;
else if(ID[i]==candidate3.id)
candidate3.count++;
else if(candidate1.count==0){
candidate1.id=ID[i];
candidate1.count++;
}
else if(candidate2.count==0){
candidate2.id=ID[i];
candidate2.count++;
}
else if(candidate3.count==0){
candidate3.id=ID[i];
candidate3.count++;
}
else{
candidate1.count--;
candidate2.count--;
candidate3.count--;
}
}
cout<<candidate1.id<<endl;
cout<<candidate2.id<<endl;
cout<<candidate3.id<<endl;
system("pause");
return 0;
}