1 条题解
-
0
问题分析:
- 需要根据复杂规则确定每个人的投票
- 处理规则优先级和冲突
- 统计票数并确定结果
关键点:
- 规则条件判断
- 投票冲突处理
- 票数统计
解题步骤:
- 解析输入,获取在场朋友名单
- 按规则顺序检查每个人的投票
- 处理规则冲突(弃权)
- 统计各地点票数
- 确定最高票数结果
#include <iostream> #include <vector> #include <string> #include <map> #include <algorithm> using namespace std; string getVote(const string& name, const vector<string>& present) { bool anne = find(present.begin(), present.end(), "Anne") != present.end(); bool bob = find(present.begin(), present.end(), "Bob") != present.end(); bool karin = find(present.begin(), present.end(), "Karin") != present.end(); bool charly = find(present.begin(), present.end(), "Charly") != present.end(); bool dave = find(present.begin(), present.end(), "Dave") != present.end(); bool edward = find(present.begin(), present.end(), "Edward") != present.end(); // 移除未使用的变量声明 // bool frank = find(present.begin(), present.end(), "Frank") != present.end(); if (name == "Anne") { if (charly && anne) return "cinema"; // Rule 6 return "cinema"; // Rule 1 } if (name == "Bob") { if (dave) return "cocktail bar"; // Rule 5 if (edward) return "cocktail bar"; // Rule 8 if (!anne) return "cocktail bar"; // Rule 10 if (karin) return "disco"; // Rule 2 return ""; // No rule applies } if (name == "Karin") { if (charly) return "disco"; // Rule 3 if (anne) return "cinema"; // Rule 3 return "cocktail bar"; // Rule 3 } if (name == "Dave") { return ""; // Rule 4 } if (name == "Edward") { if (anne && !charly) return "cocktail bar"; // Rule 7 return "cinema"; // Rule 7 } if (name == "Frank") { if (!bob && !anne) return "cinema"; // Rule 9 if (anne) return "disco"; // Rule 9 return ""; // No rule applies } return ""; } void solveScenario(int scenarioNum, const vector<string>& names) { map<string, int> votes; votes["cinema"] = 0; votes["cocktail bar"] = 0; votes["disco"] = 0; // 使用传统for循环替代范围for循环 for (vector<string>::const_iterator it = names.begin(); it != names.end(); ++it) { const string& name = *it; string vote = getVote(name, names); if (!vote.empty()) { votes[vote]++; } } cout << "Scenario #" << scenarioNum << ":" << endl; // 手动计算最大值替代初始化列表 int maxVotes = max(votes["cinema"], max(votes["cocktail bar"], votes["disco"])); int countMax = 0; string result; if (votes["cinema"] == maxVotes) { countMax++; result = "cinema"; } if (votes["cocktail bar"] == maxVotes) { countMax++; result = "cocktail bar"; } if (votes["disco"] == maxVotes) { countMax++; result = "disco"; } if (countMax > 1 || maxVotes == 0) { cout << "stay at the Hacienda" << endl; } else { cout << result << endl; } cout << endl; } int main() { int scenarios; cin >> scenarios; cin.ignore(); // Ignore newline after scenarios count for (int i = 1; i <= scenarios; ++i) { string line; getline(cin, line); vector<string> names; size_t pos = 0; while ((pos = line.find(' ')) != string::npos) { names.push_back(line.substr(0, pos)); line.erase(0, pos + 1); } if (!line.empty()) { names.push_back(line); } solveScenario(i, names); } return 0; }
- 1
信息
- ID
- 806
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者