思路:根据老师上课提示的“相互消去”的方法,因为水王的发帖子的数量超过一半,所以两个不同的帖子消去,最后剩下的肯定就是水王的id
#include<iostream.h> #include "stdlib.h" int main() { int length; int Idnum[3]={0,0,0}; int ID[3]={0,0,0}; cout<<"请输入总帖子数量:"<<endl; cin>>length; int * curId=new int [length]; cout<<"请输入水军的ID列表"<<endl; for(int j=0;j<length;j++) { cin>>curId[j]; } for(int i=0;i<length;i++) { if(Idnum[0]==0 && curId[i]!=ID[1] && curId[i]!=ID[2]) { Idnum[0]=1; ID[0]=curId[i]; } else if(Idnum[1]==0 && curId[i]!=ID[0] && curId[i]!=ID[2]) { Idnum[1]=1; ID[1]=curId[i]; } else if(Idnum[2]==0 && curId[i]!=ID[0] && curId[i]!=ID[1]) { Idnum[2]=1; ID[2]=curId[i]; } else if(curId[i]!=ID[0] && curId[i]!=ID[1] && curId[i]!=ID[2]) { Idnum[0]--; Idnum[1]--; Idnum[2]--; } else if(curId[i]==ID[0]) { Idnum[0]++; } else if(curId[i]==ID[1]) { Idnum[1]++; } else if(curId[i]==ID[2]) { Idnum[2]++; } } cout<<"三个水桶ID为:"<<ID[0]<<" "<<ID[1]<<" "<<ID[2]<<" "<<endl;+ return 0; }