找水王2
1.设计思路:这次要找3个水王,所以我这次在上次的基础上设置了3个水王的权值。
2.代码
#include <iostream> #include<string> using namespace std; #define N 100 int main() { int n, i, X[N]; string ID[N]; cout << "请输入帖子总数:" << endl; cin >> n; cout << "请输入帖子序号以及其相对应的ID:" << endl; for (i = 0; i < n; i++) { cin >> X[i]; cin >> ID[i]; } cout << "帖子表为:" << endl; cout << "序号" << '\t' << "ID" << endl; for (i = 0; i < n; i++) { cout << "\t" << X[i] << '\t' << ID[i] << endl; } int quanzhi[3] = { 0 }, water[3] = { 0,1,2 }; for (int i = 0; i<n; i++) { if (ID[water[0]] == ID[i]) { quanzhi[0]++; } else if (ID[water[1]] == ID[i]) { quanzhi[1]++; } else if (ID[water[2]] == ID[i]) { quanzhi[2]++; } else if (quanzhi[0] == 0) { ID[water[0]] = ID[i]; } else if (quanzhi[1] == 0) { ID[water[1]] = ID[i]; } else if (quanzhi[2] == 0) { ID[water[2]] = ID[i]; } else { quanzhi[0]--; quanzhi[1]--; quanzhi[2]--; } } for (int i = 0; i<3; i++) { cout << "第 " << i + 1 << " 个水王是:" << ID[water[i]] << endl; } }
3.程序截图