模拟、字符串--P1042 乒乓球 题解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37037492/article/details/86575445

P1042 乒乓球
字符串string的基本使用

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <ctime>

using namespace std;
static const auto y = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    return 0;
}();

void pro1();
int main() {
    pro1();
    return 0;
}
void pro1() {
    string str;
    vector<string> strs;
    strs.reserve(2500);
    while (true){
        getline(cin, str);
        if(str.length()==0)break;
        strs.push_back(str);
    }
    int role1=0,role2=0;
    bool flag = true;
    for (int i = 0; i < strs.size(); ++i) {
        for (int j = 0; j < strs[i].size(); ++j) {
            if(strs[i][j]=='E'){
                cout<<role1<<":"<<role2;
                role1=0,role2=0;
                flag = false;
                break;
            }
            strs[i][j]=='W'?role1+=1:NULL;
            strs[i][j]=='L'?role2+=1:NULL;
            if((role1>=11||role2>=11) && abs(role1-role2) >= 2){
                cout<<role1<<":"<<role2<<endl;
                role1=0,role2=0;
            }
        }
        if(!flag)break;
    }
    flag = true;
    cout<<endl;
    cout<<endl;
    for (int i = 0; i < strs.size(); ++i) {
        for (int j = 0; j < strs[i].size(); ++j) {
            if(strs[i][j]=='E'){
                cout<<role1<<":"<<role2;
                role1=0,role2=0;
                flag = false;
                break;
            }
            strs[i][j]=='W'?role1+=1:NULL;
            strs[i][j]=='L'?role2+=1:NULL;
            if((role1>=21||role2>=21) && abs(role1-role2) >= 2){
                cout<<role1<<":"<<role2<<endl;
                role1=0,role2=0;
            }
        }
        if(!flag)break;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_37037492/article/details/86575445
今日推荐