寻找发帖水王02
题目:随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有3个发帖很多的ID。据统计他们的发帖数目都超过了帖子总数目的1/4,你能从发帖列表中快速找出他们吗?
设计思路:
水王01只需要一个结果,而现在需要3个结果,上题用到的nTimes,也应改为3个计数器。现在需要3个变量来记录当前遍历过的3个不同的ID,而nTimes的3个元素分别对应当前遍历过的3个ID出现的个数。如果遍历中有某个ID不同于这3个当前ID,就判断当前3个ID是否有某个的nTimes为0,如果有,那这个新遍历的ID就取而代之,并赋1为它的遍历数(即nTimes减1),如果当前3个ID的nTimes皆不为0,则3个ID的nTimes皆减去1。
实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | //寻找发帖水王02 //胡浩特 2016年5月26日 #include <iostream> using namespace std; int candidate[3]; int ntime[3] = { 0 }; int input[100]; int num = 0; int main() { cout << "please input" << endl; int t; while (cin >> t) { if (t == -1) break ; input[num++] = t; } bool flag = false ; for ( int i = 0; i < num; i++) { flag = false ; for ( int j = 0; j < 3; j++) { if (ntime[j] == 0) { continue ; } if (candidate[j] == input[i]) { ntime[j]++; flag = true ; } } if (flag == true ) { continue ; } for ( int j = 0; j < 3; j++) { if (ntime[j] == 0) { candidate[j] = input[i]; ntime[j]++; flag = true ; break ; } } if (flag == true ) { continue ; } for ( int j = 0; j < 3; j++) { ntime[j]--; } } cout << ntime[0] << " " << ntime[1] << " " << ntime[2] << endl; cout << candidate[0] << " " << candidate[1] << " " << candidate[2] << endl; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步