Jennifer, a preschooler, loves playing rock-paper-scissors with her older brother because it's one of the few games she has a chance of winning against him. Every day when she gets home, Jennifer asks her brother to play rock-paper-scissors with her. To beat her brother, Jennifer spends a lot of time studying strategies for throwing hand signs at kindergarten and writing down the signs she plans to throw on paper. However, as her brother gets busier with his junior high school homework, he doesn't have time to think about what signs to throw first. So, he decides to determine his throws based on the signs Jennifer throws.
Every day, her brother only needs to decide the condition F for the first round of rock-paper-scissors. After that, his strategy for rock-paper-scissors is as follows:
- If Jennifer throws the same hand sign in two consecutive rounds, he will throw the hand sign that beats Jennifer's hand sign from the previous two rounds.
- Otherwise, he will throw the same hand sign as Jennifer did in the previous round.
請你寫一個程式模擬兩人遊戲過程與結果。
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line inputs the hand sign F that Jennifer's brother will throw in the first round.
The second line inputs the number N of hand signs Jennifer is prepared to throw. The third line inputs the hand signs y₁, y₂, ..., yₙ that Jennifer is prepared to throw, separated by spaces. | The output consists of one line, sequentially outputting the hand sign that Jennifer's brother throws in each round, separated by spaces. After a colon, output the round number at which the winner is determined. If her brother wins in round k, output 'Won at round k'. If her brother loses in round k, output 'Lost at round k'. If the game is a draw after N rounds, output 'Drew at round N'. |
0 4 2 5 0 2 | 0 : Won at round 1 |
2 2 2 0 | 2 2 : Lost at round 2 |
5 4 5 5 0 0 | 5 5 2 : Lost at round 3 |
5 6 5 5 2 2 0 0 | 5 5 2 2 0 0 : Drew at round 6 |
Thought Process
You need to collect the hand signs thrown by Jennifer into an array to determine what hand sign her brother should throw each time. When collecting the array, check how many numbers have been received. If the second number is received, the brother throws the hand sign corresponding to the first number. If more than two numbers have been received, check if the previous two numbers are the same.
The game ends whenever there is a win or loss, and the subsequent data is not used. However, input is still required. Therefore, a Boolean value finish can be declared with a default value of false. If a win or loss occurs, finish is set to true. After each data collection in the for loop, check if finish == true and continue to skip the subsequent checks.
Since there is also a possibility of an eternal draw, after the for loop ends, the value of finish needs to be checked. If !finish, it means that the outcome has not been decided yet, and "Drew at round N" should be printed, where N is the current round number.
Sample Code-ZeroJudge H026: Rock, paper, scissors!
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int F, N;
cin >> F >> N;
vector<int>num;
map<int, int>MAP;
MAP[0] = 5;
MAP[2] = 0;
MAP[5] = 2;
bool finish = false;
for (int i = 0; i<N; i++) {
int tmp;
cin >> tmp;
if (finish) continue;
num.push_back(tmp);
if (i == 1) F = num[0];
else if (i > 1) {
if (num[i-2] == num[i-1]) F = MAP[num[i-2]];
else F = num[i-1];
}
cout << F << " ";
if ((F == 0 && tmp == 5) || (F == 2 && tmp == 0) || (F == 5 && tmp == 2)) {
cout << ": Lost at round " << i+1 << "\n";
finish = true;
continue;
}
if ((F == 0 && tmp == 2) || (F == 2 && tmp == 5) || (F == 5 && tmp == 0)) {
cout << ": Won at round " << i+1 << "\n";
finish = true;
}
}
if (!finish) cout << ": Drew at round " << N << "\n";
}
//ZeroJudge H026
//Dr. SeanXD