问题描述
p489原题链接:UVaOJ 489 - Hangman Judge
相关说明:本题为《算法竞赛入门经典(第2版)》例题 4-2
解法一:模拟 + 集合
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 |
#include <bits/stdc++.h> using namespace std; int main() { int round; string sol, seq; while (cin >> round && round != -1) { cout << "Round " + to_string(round) << endl; cin >> sol, cin >> seq; int stroke = 0, status = 0; set<char> letter; for (auto c : sol) letter.insert(c); for (auto c : seq) { if (letter.find(c) != letter.end()) { letter.erase(c); if (letter.empty()) { cout << "You win." << endl; status = 1; break; } } else { stroke += 1; if (stroke == 7) { cout << "You lose." << endl; status = 2; break; } } } if (!status) cout << "You chickened out." << endl; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
while True: round_num = input() if int(round) == -1: break print("Round", round_num) sol, seq = input(), input() letter, stroke, status = set(), 0, 0 for c in sol: letter.add(c) for c in seq: if c in letter: letter.remove(c) if len(letter) == 0: print("You win.") status = 1 break else: stroke += 1 if stroke == 7: print("You lose.") status = 2 break if not status: print("You chickened out.") |