编程之美2.3 寻找发帖“水王”

来源:互联网 发布:4g网络优化先培训多久 编辑:程序博客网 时间:2024/05/16 15:30

 扩展问题:
找出有3个发帖很多的ID,他们的发帖数目都超过了总数目N的1/4.你能从发帖ID列表中快速找出他们的ID吗?

#include<iostream>using namespace std;void find(int id[], int N){int box[4];int count[4];memset(count, 0, sizeof(count));int i = 0;for(i = 0; i < N; i++){int j = 0;for(j = 0; j < 4; j++){if(count[j] > 0 && box[j] == id[i]){count[j]++;break;}}if(j == 4){for(j = 0; j < 4; j++){if(count[j] == 0){box[j] = id[i];count[j]++;break;}}}for(j = 0; j < 4; j++){if(count[j] < 1)break;}if(j == 4){for(j = 0; j < 4; j++){count[j]--;if(count[j] == 0)box[j] = 0;}}}for(i = 0; i < 4; i++){if(count[i] > 0)cout << box[i] << endl;}}int main(){freopen("test.txt", "r", stdin);int tempid = 0;int i = 0;int id[100];while(cin >> tempid)id[i++] = tempid;find(id, i);return 0;}


 

原创粉丝点击