首页 > 试题广场 >

乒乓球

[编程题]乒乓球
  • 热度指数:4196 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}旺仔哥哥特别喜欢打乒乓球,有一天他给你一串由字符 \texttt{W}\texttt{L} 组成的比赛记录,\texttt{W} 表示旺仔哥哥得分,\texttt{L} 表示对手得分。

\hspace{15pt}请分别按11 分制21 分制统计比赛结果,并输出每局比分。规则如下:
\hspace{23pt}\bullet\, 当且仅当一局比赛中存在某个选手分数不小于 11(或 21)且双方比分差 \geqq 2 时,判定一局结束,此时得分高的选手获胜;
\hspace{23pt}\bullet\, 若读取结束时当前局未结束,也需输出当前比分;
\hspace{23pt}\bullet\, 新局开始时比分记为 0{:}0

输入描述:
\hspace{15pt}一行一个字符串 s\left( 1 \leqq |s| \leqq 10^5,s_i \in \{\text{W},\text{L}\}\right),表示比赛记录。


输出描述:
\hspace{15pt}输出两部分,每部分若干行,每行为一局的比分,形如 \text{旺仔哥哥的得分}:\text{对手得分},按照记录顺序:
\hspace{23pt}\bullet\, 第一部分为 11 分制结果;
\hspace{23pt}\bullet\, 空行分隔后,第二部分为 21 分制结果。
示例1

输入

WWWWWWWWWWWWWWWWWWWWWWLW

输出

11:0
11:0
1:1

21:0
2:1

精简写法

#include <iostream>
using namespace std;


void win(int d, string s) {

    int w = 0, l = 0;

    for (int i = 1; i <= s.size(); i++) {

        if (s[i - 1] == 'W') {
            w++;
        } else if (s[i - 1] == 'L') {
            l++;
        }

        if ((w >= d || l >= d) &&  abs(w - l) >= 2) {
            cout << w << ':' << l << endl;
            w = 0;
            l = 0;

        }

    }
    cout << w << ':' << l << endl;
}


int main() {
    string s;
    cin >> s;
    win(11, s);
    cout << endl;
    win(21, s);
}
发表于 2025-08-05 15:39:22 回复(0)
import sys


def t(s, r):
    w, l = 0, 0
    for i in s:
        if i == "W":
            w += 1
        else:
            l += 1
        if ((w >= r) + (l >= r)) and abs(w - l) >= 2:
            print(f"{w}:{l}")
            w, l = 0, 0
    print(f"{w}:{l}")


s = sys.stdin.readline().strip()
t(s, 11)
print()
t(s, 21)

发表于 2026-01-27 14:24:04 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        scanner.close();
        
        // 计算11分制结果
        List<String> result11 = calculateScore(s, 11);
        // 计算21分制结果
        List<String> result21 = calculateScore(s, 21);
        
        // 输出11分制结果
        for (String score : result11) {
            System.out.println(score);
        }
        
        // 空行分隔
        System.out.println();
        
        // 输出21分制结果
        for (String score : result21) {
            System.out.println(score);
        }
    }
    
    /**
     * 根据指定的分制计算比赛结果
     * @param s 比赛记录字符串
     * @param target 分制(11或21)
     * @return 每局比分的列表
     */
    private static List<String> calculateScore(String s, int target) {
        List<String> result = new ArrayList<>();
        int wScore = 0; // 旺仔哥哥的得分
        int lScore = 0; // 对手的得分
        
        for (char c : s.toCharArray()) {
            // 更新得分
            if (c == 'W') {
                wScore++;
            } else {
                lScore++;
            }
            
            // 检查当前局是否结束
            if (isGameOver(wScore, lScore, target)) {
                // 记录本局比分
                result.add(wScore + ":" + lScore);
                // 开始新局,重置分数
                wScore = 0;
                lScore = 0;
            }
        }
        
        // 处理未结束的局&nbs***bsp;新开的局
        if (wScore > 0 || lScore > 0) {
            result.add(wScore + ":" + lScore);
        } else if (wScore == 0 && lScore ==0){
            result.add(0 + ":" + 0);    
        }
        
        return result;
    }
    
    /**
     * 判断当前局是否结束
     * @param w 旺仔哥哥的得分
     * @param l 对手的得分
     * @param target 分制(11或21)
     * @return 若局结束则返回true,否则返回false
     */
    private static boolean isGameOver(int w, int l, int target) {
        // 当且仅当一局比赛中存在某个选手分数不小于target且双方比分差≥2时,判定一局结束
        return (w >= target || l >= target) && Math.abs(w - l) >= 2;
    }
}
    

发表于 2025-08-29 08:50:01 回复(0)
为什么都0:0了还要输出呢 这不是没有了吗
发表于 2025-07-12 08:58:28 回复(5)
啊这是什么情况?
发表于 2026-01-23 22:35:34 回复(0)
//小白也能一眼看懂的代码
#include<iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;
    int scorew1 = 0, scorel1 = 0;
    int start = 0;
    bool switch1 = false;
    for (int i = start; i < s.size(); i++) {
        if (s[i] == 'W') {
            scorew1++;
            switch1 = false;
        } else if (s[i] == 'L') {
            scorel1++;
            switch1 = false;
        }
        if ((scorel1 >= 11 && scorel1 - scorew1 >= 2) || scorew1 >= 11 &&
                scorew1 - scorel1 >= 2) {
            cout << scorew1 << ':' << scorel1 << '\n';
            scorel1 = 0, scorew1 = 0;
            switch1 = true;
        }
    }
    if (!switch1) {
        cout << scorew1 << ':' << scorel1 << '\n';
    }
    else cout<<"0:0"<<'\n';

    cout << '\n';

    int scorew2 = 0, scorel2 = 0;
    int start2 = 0;
    bool switch2 = false;
    for (int i = start2; i < s.size(); i++) {
        if (s[i] == 'W') {
            scorew2++;
            switch2 = false;
        } else if (s[i] == 'L') {
            scorel2++;
            switch2 = false;
        }
        if ((scorel2 >= 21 && scorel2 - scorew2 >= 2) || scorew2 >= 21 &&
                scorew2 - scorel2 >= 2) {
            cout << scorew2 << ':' << scorel2 << '\n';
            scorel2 = 0, scorew2 = 0;
            switch2 = true;
        }
    }
    if (!switch2) {
        cout << scorew2 << ':' << scorel2 << '\n';
    }
    else cout<<"0:0"<<'\n';

}
发表于 2026-01-22 19:41:56 回复(0)
#include <iostream>
using namespace std;
#include<string>
#include<cmath>
int main() {
   string s;
   cin>>s;
    int win_W=0,win_L=0;
   for(int i=0;i<s.size();i++){
        if(s[i]=='W'){
            win_W++;
        }
        else{
            win_L++;
        }
       if((win_W>=11||win_L>=11)&&abs(win_L-win_W)>=2){
           cout<<win_W<<":"<<win_L<<endl;
           win_W=0;
           win_L=0;
       }
       if(i==s.size()-1){
           cout<<win_W<<":"<<win_L<<endl;
           win_W=0;
           win_L=0;
       }
   }
   cout<<endl;
   win_W=0;
   win_L=0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='W'){
            win_W++;
        }
        else{
            win_L++;
        }
       if((win_W>=21||win_L>=21)&&abs(win_L-win_W)>=2){
           cout<<win_W<<":"<<win_L<<endl;
           win_W=0;
           win_L=0;
       }
       if(i==s.size()-1){
           cout<<win_W<<":"<<win_L<<endl;
           win_W=0;
           win_L=0;
       }
   }
}

发表于 2026-01-16 17:02:37 回复(0)
import math

def main():
    s = input()

    w_1,w_2 = 0,0
    l_1,l_2 = 0,0
    ans_1 = []
    ans_2 = []
    for c in s:
        if 'W' == c:
            w_1+=1
            w_2+=1
        elif 'L' == c:
            l_1+=1
            l_2+=1
        if (w_1 >= 11&nbs***bsp;l_1 >= 11) and abs(w_1 - l_1) >= 2:
            ans_1.append((w_1,l_1))
            w_1 = l_1 = 0
        if (w_2 >= 21&nbs***bsp;l_2 >= 21) and abs(w_2 - l_2) >= 2:
            ans_2.append((w_2,l_2))
            w_2 = l_2 = 0
    if w_1 + l_1 >= 0:  
        ans_1.append((w_1, l_1))
    if w_2 + l_2 >= 0:  
        ans_2.append((w_2, l_2))
    for i in range(len(ans_1)):
        print(f'{ans_1[i][0]}:{ans_1[i][1]}')
    print()
    for i in range(len(ans_2)):
        print(f'{ans_2[i][0]}:{ans_2[i][1]}')
        

if __name__ == '__main__':
    main()

发表于 2025-11-19 16:04:06 回复(0)
def result(s,fenzhi):
    score_W = 0
    score_L = 0
    for i in s:
        if i=='W':
            score_W += 1
        elif i=='L':
            score_L += 1
        if (score_W>=fenzhi or score_L>=fenzhi) and abs(score_W-score_L)>=2:
            print(f'{score_W}:{score_L}')
            score_W = 0
            score_L = 0
    print(f'{score_W}:{score_L}')

s = input()
result(s,11)
print('')
result(s,21)
发表于 2025-08-25 12:37:00 回复(0)