Daily practice 2023.12.8 - A sure win [PTA]

Question link: L1-044 A sure win

Topic requirements:

Everyone should be able to play the "hammer, paper, scissors" game: two people give gestures at the same time, and the winning and losing rules are as shown in the picture:

You are now required to write a program that ensures a win without losing, and gives you the corresponding winning moves based on the opponent's moves. but! In order not to let the opponent lose too badly, you need to give up a draw every K times.

Input format:

The input first gives a positive integer K (≤10) in the first line, that is, the number of tie intervals. Each row then gives one of the opponent's moves: ChuiZi represents "hammer", JianDao represents "scissors", Bu represents " cloth". End represents the end of input. Do not treat this line as a move.

Output format:

For each input move, a move that guarantees a win or a draw will be output as required. Each move occupies one line.

Input example:

2
ChuiZi
JianDao
Bu
JianDao
Bu
ChuiZi
ChuiZi
End

Output sample:

Bu
ChuiZi
Bu
ChuiZi
JianDao
ChuiZi
Bu

Idea:

1. First determine how many draws there are

2. Loop through input of opponent’s moves

3.l = (i + 1) % (n + 1)Use the loop remainder method to determine whether it is a tie a>

4. If(i + 1) % (n + 1) is an integer, it proves that it is not a tie, otherwise it is a tie

Code:


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int k;
    cin >> k;
    string x;
    int i = 0;
    while(cin >> x)
    {
        if(x == "End")
            break;
        int l = (i + 1) % (k + 1);
        if(l)
        {
            if(x == "ChuiZi")
                cout << "Bu" << endl;
            else if(x == "JianDao")
                cout << "ChuiZi" << endl;
            else if(x == "Bu")
                cout << "JianDao" << endl;
        }
        else
        {
            cout << x << endl;
        }
        i ++;
    }
    return 0;
}

Test Results: 

Guess you like

Origin blog.csdn.net/m0_63168877/article/details/134866159