P1042 乒乓球

P1042 乒乓球
严格按照题目要求去做即可(最好自己在本子上画个流程图)

#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> pii;
const int N=2505;
pii eleven={0,0},twenty_one={0,0};
string nums[N]={""};
int length=0;

int main() {
    vector<pii>ele,twenty;
//    ofstream out("1.txt",ios::app);
//    if (out.fail())
//        cout<<"error\n"<<endl;
//    out.clear();
    while (1){
        string str;
        cin>>str;
        length=str.length();
        for (int i = 0; i < length ; ++i) {
            if (str[i]=='E'){
//                out<<eleven.first<<":"<<eleven.second<<endl;
                ele.push_back(eleven),eleven=pii(0,0);
//                out<<twenty_one.first<<":"<<twenty_one.second<<endl;
                twenty.push_back(twenty_one),twenty_one=pii(0,0);
                goto setup1;
            }
            else if (str[i]=='W')
                eleven.first++,twenty_one.first++;
            else
                eleven.second++,twenty_one.second++;
            if (eleven.first>=11||eleven.second>=11){
                if (abs(eleven.first-eleven.second)>=2){
                    //out<<eleven.first<<":"<<eleven.second<<endl;
                    ele.push_back(eleven),eleven=pii(0,0);
                }
            }
            if (twenty_one.first>=21||twenty_one.second>=21){
                if (abs(twenty_one.first-twenty_one.second)>=2){
//                    out<<twenty_one.first<<":"<<twenty_one.second<<endl;
                    twenty.push_back(twenty_one),twenty_one=pii(0,0);
                }
            }
        }
    }
    setup1:
    for (auto i = ele.begin(); i !=ele.end() ; ++i) {
        cout<<i->first<<":"<<i->second<<endl;
    }
    cout<<endl;
    for (auto i = twenty.begin(); i !=twenty.end() ; ++i) {
        cout<<i->first<<":"<<i->second<<endl;
    }
//    out.close();
    return 0;
}```

### 关于洛谷 P1042 乒乓球问题的解题思路 对于该问题,核心在于模拟两个不同计分制度下的比赛过程。给定一系列字符作为输入,其中`W`代表华华得分,`L`代表平平得分,而`E`则表示当前局结束并进入下一局。 #### 输入处理逻辑 程序需逐个读取字符直至遇到`E`为止,在此期间依据所遇字符更新对应选手分数,并根据不同赛制规则判定是否某一方赢得了一局以及何时应开启新的一局[^2]。 #### 不同赛制下的胜负条件 - **11分制**:当任意一名玩家达到11分且领先对手至少两分时即获胜;若双方均达10分以上但差距不足两分,则继续游戏直到满足上述胜利条件。 - **21分制**:同样遵循先得规定分数(这里是21)且超出对方不少于两分者胜出的原则,只是起始目标分数更高一些[^3]。 ### C语言代码实现 下面给出一段完整的C语言源码用于解决这个问题: ```c #include <stdio.h> #define MAXN 1005 char game[MAXN]; int score_11[2], score_21[2]; void reset_scores() { score_11[0] = score_11[1] = 0; score_21[0] = score_21[1] = 0; } // 判断是否在一场比赛中达到了可以赢的状态 bool can_win(int *score, int target) { return (score[0]>=target || score[1]>=target) && abs(score[0]-score[1])>=2; } int main(){ while(scanf("%s",game)!=EOF){ bool is_end=false; char winner=' '; reset_scores(); for(int i=0; !is_end&&game[i]!='\0'; ++i){ switch(game[i]){ case 'W': ++score_11[0]; ++score_21[0]; break; case 'L': ++score_11[1]; ++score_21[1]; break; case 'E': if(can_win(score_11, 11)){ printf("11:%c %d-%d\n",(winner=(score_11[0]>score_11[1])?'H':'P'),score_11[0],score_11[1]); reset_scores(); // 准备下一个回合 } if(can_win(score_21, 21)){ printf("21:%c %d-%d\n", (winner=(score_21[0]>score_21[1])?'H':'P'),score_21[0],score_21[1]); reset_scores(); // 准备下一个回合 } is_end=true; break; } if(!is_end){ // 如果不是最后一轮打印中间状态 printf("11:"); if(winner==' ')printf("%d-%d\n",score_11[0],score_11[1]); else printf("-%d-%d\n",score_11[0],score_11[1]); printf("21:"); if(winner==' ')printf("%d-%d\n",score_21[0],score_21[1]); else printf("-%d-%d\n",score_21[0],score_21[1]); } } } } ``` 这段代码实现了对每一轮比赛中两位参赛者的得分情况进行记录,并按照两种不同的记分方式分别计算最终成绩。每当检测到一局结束符`E`时,会检查是否有选手符合各自赛制下的获胜标准,并据此输出相应结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值