Luo Gu problem solution --P1042: Table Tennis

Related topics

Topic Link

Luo Valley, https://www.luogu.com.cn/problem/P1042 .

My OJ, http://47.110.135.197/problem.php?id=4746 .

Title Description

China and China carried out by the analysis, the first ball of the outcome of each game uses a list, and then were calculated under the 11-point and 21-point scale, competition results from the two (as of end of record).
For example, now have such a record, (wherein W represents China and China to get a point, L represents a Chihuahua opponent gains a point):
WWWWWWWWWWWWWWWWWWWWWWLW
under the 11-point scale, when the result of the game is China and China the first game 11 to 0 victory , 0 win the second game 11, the third game in progress, the current score of 11. In the 21-point scale, this time the game was Chihuahua 21 0 win first game, the second game in progress, score of 2 to 1. If a game has just started, this time the score was 0-0. Minutes until the difference is equal to or greater than 2, only the end of a game.
Your program is to output the correct result of a series of games for input information (WL form).

Input Format

Each file contains a plurality of input lines of character strings, the string of uppercase W, L and E form. Where E represents the end of the game information, the program should ignore everything after the E.

Output Format

Output consists of two parts, each part has a plurality of rows, each row corresponding to the score of a game (game information input by the order). Wherein the first portion is the result in the 11-point scale, results in the second portion is made 21 points, separated by a blank line between the two parts.

SAMPLE INPUT

WWWWWWWWWWWWWWWWWWWW
WWLWE

Sample Output

11:0
11:0
1:1

21:0
2:1

Explanation

Up to 25 characters per line, up to 2500 lines.

Topic analysis

Analysis of the meaning of problems

This is a simulation questions. The character input, analyzes the corresponding game score.

Sample data analysis

According to the input sample, we have listed below a table, the simulation analysis results of the entire input.

  11-point score 21-point score
W 1:0 1:0
W 2:0 2:0
W 3:0 3:0
W 4:0 4:0
W 5:0 5:0
W 6:0 6:0
W 7:0 7:0
W 8:0 8:0
W 9:0 9:0
W 10:0 10:0
W 11:0 11:0
W 1:0 12:0
W 2:0 13:0
W 3:0 14:0
W 4:0 15:0
W 5:0 16:0
W 6:0 17:0
W 7:0 18:0
W 8:0 19:0
W 9:0 20:0
W 10:0 21:0
W 11:0 1:0
L 0:1 1:1
W 1:1 2:1
E    

From the table, we can see the changes in the score. When the input E, we follow the required output score.

Algorithmic details

How to save the score?

I use the STL queue to save the score. The main is too lazy to think.

End judge each round?

There is a small pit margin to pay special attention to at least two points, i.e. points difference is equal to or greater than 2. That is, if a 11-point, at least one of 11 points or more, and a margin of 2 minutes. Similar 21-point determination method. I use the following wording:

if ((ans1[0]>=11 || ans1[1]>=11) && abs(ans1[0]-ans1[1])>=2) {
    ...
}

AC reference code

#include <iostream>
#include <queue>
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>

using namespace std;

int main() {
    char ch;
    int ans1[2]={};//11分制比分, ans1[0]第一个人比分, ans1[1]第二人比分
    int ans2[2]={};//21分制比分

    queue<string> q1;//11分结果
    queue<string> q2;//21分结果

    while (cin>>ch) {
        if ('E'==ch) {
            ostringstream ostr1;
            ostr1 << ans1[0] << ":" << ans1[1];
            q1.push(ostr1.str());

            ostringstream ostr2;
            ostr2 << ans2[0] << ":" << ans2[1];
            q2.push(ostr2.str());

            break;
        } else {
            if ('W'==ch) {
                //Win
                ans1[0]++;
                ans2[0]++;
            } else if ('L'==ch) {
                //Los
                ans1[1]++;
                ans2[1]++;
            }

            //统计11分制结果
            if ((ans1[0]>=11 || ans1[1]>=11) && abs(ans1[0]-ans1[1])>=2) {
                ostringstream ostr;
                ostr << ans1[0] << ":" << ans1[1];
                q1.push(ostr.str());
                ans1[0] = 0;
                ans1[1] = 0;
            }

            //统计21分制结果
            if ((ans2[0]>=21 || ans2[1]>=21) && abs(ans2[0]-ans2[1])>=2)  {
                ostringstream ostr;
                ostr << ans2[0] << ":" << ans2[1];
                q2.push(ostr.str());
                ans2[0] = 0;
                ans2[1] = 0;
            }
        }
    }

    //输出
    while (false==q1.empty()) {
        cout << q1.front() << endl;
        q1.pop();
    }
    cout << endl;
    while (false==q2.empty()) {
        cout << q2.front() << endl;
        q2.pop();
    }

    return 0;
}

Simulation title, no major difficulty, step by step, as long as required to write code that can, perhaps some topics the code a bit more, but there is no difficulty.

Published 235 original articles · won praise 289 · Views 1.07 million +

Guess you like

Origin blog.csdn.net/justidle/article/details/104865808