题解 P1042 【乒乓球】

· · 题解

发现你们的代码都好长啊而且又难看懂~

基本解题思路:

题目蛮简单的,又没有什么硬性要求,内存顺便用,时间也足够,这时候就是看思路的了。

解决方案好,代码量少,写简单点别折磨自己。。。。。

#include<iostream>
#include<cmath>
using namespace std;

char str[100010];
int cnt=0;

void show(int n){

    int a=0,b=0;
    for(int i=0;i<cnt;i++){
        if(str[i]=='W') a++;
        if(str[i]=='L') b++;

        if((a>=n||b>=n)&&abs(a-b)>=2){
            cout<<a<<":"<<b<<endl;
            a=b=0;
        }
    }

    //新的一轮刚开始,或上一局没有打完
    cout<<a<<":"<<b<<endl;
}

int main(){
    char ch;

    while(cin>>ch&&ch!='E'){
        if(ch=='W'||ch=='L'){
          str[cnt++]=ch;
        }
    }

    show(11);
    cout<<endl;
    show(21);
}