30求助,悬关

P1042 [NOIP2003 普及组] 乒乓球

LGZX @ 2024-09-29 18:26:22

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(void) {
    string inp;
    vector<char> res;
    while (cin >> inp) {
        for (char c : inp) {
            if (c == 'E') {
                break;
            }
            res.push_back(c);
        }
        if (inp.back() == 'E') {
            break;
        }
    }
    int s11_h = 0, s11_o = 0;
    int s21_h = 0, s21_o = 0; 
    vector<string> r11;
    vector<string> r21;
    for (char r : res) {
        if (r == 'W') {
            s11_h++;
            s21_h++;
        }
        else if (r == 'L') {
            s11_o++;
            s21_o++;
        }
        if (s11_h >= 11 && s11_h - s11_o >= 2) {
            r11.push_back(to_string(s11_h) + ":" + to_string(s11_o));
            s11_h = 0;
            s11_o = 0;
        }
        if (s21_h >= 21 && s21_h - s21_o >= 2) {
            r21.push_back(to_string(s21_h) + ":" + to_string(s21_o));
            s21_h = 0;
            s21_o = 0;
        }
    }
    if (s11_h > 0 || s11_o > 0) {
        r11.push_back(to_string(s11_h) + ":" + to_string(s11_o));
    }
    if (s21_h > 0 || s21_o > 0) {
        r21.push_back(to_string(s21_h) + ":" + to_string(s21_o));
    }
    for (const auto& score : r11) {
        cout << score << endl;
    }
    cout << endl;
    for (const auto& score : r21) {
        cout << score << endl;
    }
    return 0;
}

by Sheep_YCH @ 2024-09-29 18:49:28

@LGZX

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char a[66000],a11[66000],a21[66000];
int hh=0,ds=0,i=0,j,round;
bool bj=1;
int main(){
    while(a[i] != 'E'){
        i++;
        scanf("%c", &a[i]);
    }
    int i11=i,i21=i;
    for(j = 1; j <= i; j++) {
        a11[j] = a[j];
        a21[j] = a[j];
    }
    j=1;
    while(i11>0) {
        while(true) {
            if(i11<1) break;
            if((hh >= 11 && hh - ds >= 2) || (ds >= 11 && ds - hh >= 2))
                break;
            else {
                if(a11[j] == 'W') hh++;
                else if(a11[j] == 'L')ds++; 
                i11--;
                j++;
            }

        }
        cout<<hh<<":"<<ds<<endl;    
        hh=0;ds=0;  
    }
    cout<<endl;
    j=1;
    hh=0;ds=0;
    while(i21>0) {
        while(true) {
            if(i21<1) break;
            if((hh >= 21 && hh - ds >= 2) || (ds >= 21 && ds - hh >= 2))
                break;
            else {
                if(a21[j] == 'W') hh++;
                else if(a21[j] == 'L')ds++; 
                i21--;
                j++;
            }

        }
        cout<<hh<<":"<<ds<<endl;    
        hh=0;ds=0;

    }

    return 0;
}

建议使用这种方法比较简单


by LGZX @ 2024-09-29 19:43:25

@Sheep_YCH 好的,谢谢


|