标签:
题目:在上一次的基础上,大水王消失了,出现了三个小水王,并且他们发帖数均超过总数的1/4,要求:如果你有一张当前论坛的帖子(包括回帖)列表,其中帖子的作者的ID也在其中,你能快速的找到这三个小水王吗?
代码:
//找小水王 lvguanghao #include <iostream> using namespace std; #define N 100 int main() { int xiaoshuiwang[3]; int ID[N], a; int num = 0; int count[3] = { 0 }; bool flag = false; cout << "请输入ID" << endl; while (cin >> a)//输入一个数组 敲回车结束 { ID[num++] = a; if (getchar() == ‘\n‘) { break; } } for (int i = 0; i < num; i++) { if (ID[i] == 0) { cout << "输入ID数不正确" << endl; return 0; } } for (int i = 0; i < num; i++) { flag = false; for (int j = 0; j < 3; j++) { if (count[j] == 0) { continue; } if (xiaoshuiwang[j] == ID[i]) { count[j]++; flag = true; } } if (flag == true) { continue; } for (int j = 0; j < 3; j++) { if (count[j] == 0) { xiaoshuiwang[j] = ID[i]; count[j]++; flag = true; break; } } if (flag == true) { continue; } for (int j = 0; j < 3; j++) { count[j]--; } } cout << "水王ID 分别为: " << endl; cout << xiaoshuiwang[0] << " " << xiaoshuiwang[1] << " " << xiaoshuiwang[2] << endl; }
单元测试:
标签:
原文地址:http://www.cnblogs.com/lvstudy/p/5538913.html