标签:
一.题目:
#include<iostream.h> #include<assert.h> int * search(int *userId,int n) { int curId[3] = {0}; //当前保存的三个不同ID int curNum[3] = {0}; //当前三个不同ID各自的数目 for(int i = 0;i < n;i++) { if(curNum[0] == 0&&userId[i] != curId[1]&&userId[i] != curId[2]) //用户ID在当前ID中不存在相同ID且userID[0]的当前数量为0 { curNum[0] = 1; curId[0] = userId[i]; } else if(curNum[1] == 0&&userId[i] != curId[0]&&userId[i] != curId[2]) { curNum[1] = 1; curId[1] = userId[i]; } else if(curNum[2] == 0&&userId[i] != curId[0]&&userId[i] != curId[1]) { curNum[2] = 1; curId[2] = userId[i]; } else if(userId[i] == curId[0]) //用户ID和当前ID相同则当前ID数+1 { curNum[0]++; } else if(userId[i] == curId[1]) { curNum[1]++; } else if(userId[i] == curId[2]) { curNum[2]++; } else if(userId[i]!=curId[0]&&userId[i]!=curId[1]&&userId[i]!=curId[2]) //四种ID一起抵消 { curNum[0]--; curNum[1]--; curNum[2]--; } } return curId;//返回当前剩余的三个ID即为三个水王 } void main() { int n; int *userId; int curId[3] = {0}; int curNum[3] = {0}; int i; cout<<"输入总帖子数:"; cin>>n; assert(n > 0); userId=new int[n]; cout<<"输入每一条帖子的id(空格隔开):"<<endl; for(i = 0;i < n;i++) { cin>>userId[i]; } cout<<"三个水王ID是\n"; for(i = 0;i < 3;i++) //输出三个水王ID { cout<<*search(userId,n) + i<<"\t"; } }
四.截图
五.总结
通过这次试验略有收获,思维方式得到了拓宽。
标签:
原文地址:http://www.cnblogs.com/littilsaber/p/4457620.html