无语凝噎

P1042 [NOIP2003 普及组] 乒乓球

wang522024320147 @ 2024-10-18 01:45:36

//
// Created by Admin on 2024/10/17.
// 乒乓球

#include <iostream>
#include <vector>

int main(){
    int win11 = 0, lose11 = 0, win21 = 0, lose21 = 0;
    std::vector<std::pair<int, int>> use11;
    std::vector<std::pair<int, int>> use21;
    std::string str, add;
    while(std::cin >> add){
        str += add;
    }
    for(auto &c : str){
        if(c == 'W'){
            ++win11;
            ++win21;
        }else if(c == 'L'){
            ++lose11;
            ++lose21;
        }else{
            //残局记录  换行读取
            use11.emplace_back(win11, lose11);
            use21.emplace_back(win21, lose21);
            break;
        }
        if(std::abs(win11 - lose11) >= 2 && (win11 >= 11 || lose11 >=11)){
            //11分制该局比赛结束
            use11.emplace_back(win11, lose11);
            win11 = 0;
            lose11 = 0;
        }
        if(std::abs(win21 - lose21) >= 2 && (win21 >=21 || lose21 >= 21)){
            //21分制该局比赛结束
            use21.emplace_back(win21, lose21);
            win21 = 0;
            lose21 = 0;
        }
    }
    for(auto &res : use11){
        std::cout << res.first << ':' << res.second << std::endl;
    }
    std::cout << std::endl;
    for(auto &res : use21){
        std::cout << res.first << ':' << res.second << std::endl;
    }
    return 0;
}

在本地运行一直while阻塞 浪费一堆时间...


by wang522024320147 @ 2024-10-18 01:53:08

//
// Created by Admin on 2024/10/17.
// 乒乓球

#include <iostream>
#include <vector>

int main(){
    int win11 = 0, lose11 = 0, win21 = 0, lose21 = 0;
    std::vector<std::pair<int, int>> use11;
    std::vector<std::pair<int, int>> use21;
    std::string str, add;
    int flag = 0;
//    while(std::cin >> add){
//        str += add;
//    }
//    for(auto &c : str){
//        if(c == 'W'){
//            ++win11;
//            ++win21;
//        }else if(c == 'L'){
//            ++lose11;
//            ++lose21;
//        }else{
//            //残局记录  换行读取
//            use11.emplace_back(win11, lose11);
//            use21.emplace_back(win21, lose21);
//            break;
//        }
//        if(std::abs(win11 - lose11) >= 2 && (win11 >= 11 || lose11 >=11)){
//            //11分制该局比赛结束
//            use11.emplace_back(win11, lose11);
//            win11 = 0;
//            lose11 = 0;
//        }
//        if(std::abs(win21 - lose21) >= 2 && (win21 >=21 || lose21 >= 21)){
//            //21分制该局比赛结束
//            use21.emplace_back(win21, lose21);
//            win21 = 0;
//            lose21 = 0;
//        }
//    }
    while(std::getline(std::cin, str)){
        for(auto &c : str){
            if(c == 'W'){
                ++win11;
                ++win21;
            }else if(c == 'L'){
                ++lose11;
                ++lose21;
            }else{
                //残局记录
                flag = 1;
                use11.emplace_back(win11, lose11);
                use21.emplace_back(win21, lose21);
                break;
            }
            if(std::abs(win11 - lose11) >= 2 && (win11 >= 11 || lose11 >=11)){
                //11分制该局比赛结束
                use11.emplace_back(win11, lose11);
                win11 = 0;
                lose11 = 0;
            }
            if(std::abs(win21 - lose21) >= 2 && (win21 >=21 || lose21 >= 21)){
                //21分制该局比赛结束
                use21.emplace_back(win21, lose21);
                win21 = 0;
                lose21 = 0;
            }
        }
        if(flag) break;
    }
    for(auto &res : use11){
        std::cout << res.first << ':' << res.second << std::endl;
    }
    std::cout << std::endl;
    for(auto &res : use21){
        std::cout << res.first << ':' << res.second << std::endl;
    }
    return 0;
}

为什么我试图边输入边计算, 最后结果就是不对呢


by yxszcxl @ 2024-10-19 11:06:48

@wang522024320147

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

int f[2] = {11, 21};
int a[25 * 2500 + 10], n = 0;

int main() {
    char tmp;
    while (1) {
        cin >> tmp;
        if (tmp == 'E')
            break;
        else if (tmp == 'W')
            a[n++] = 1;
        else if (tmp == 'L')
            a[n++] = 0;
    }
    for (int k = 0; k < 2; k++) {
        int w = 0, l = 0;
        for (int i = 0; i < n; i++) {
            w += a[i];
            l += 1 - a[i];
            if ((max(w, l) >= f[k]) && abs(w - 1) >= 2) {
                cout << w << ":" << l << endl;
                w = l = 0;
            }
        }
        cout << w << ":" << l << endl;
        cout << endl;
    }
    return 0;
}

by guoyanwei120223 @ 2024-10-19 16:12:03

@wang522024320147 ```cpp

include<bits/stdc++.h>

using namespace std; char wl[25*2501]; int main() { char a; int w=0,l=0; for(int i=0;a!='E';i++) { cin>>a; wl[i]=a; } for(int i=0;;i++) { if(wl[i]=='W') w++; if(wl[i]=='L') l++; if(max(w,l)>=11&&abs(w-l)>=2) { cout<<w<<":"<<l<<endl; w=0; l=0; } if(wl[i]=='E') { cout<<w<<":"<<l<<endl; cout<<endl; break; } } w=0,l=0; for(int i=0;;i++) { if(wl[i]=='W') w++; if(wl[i]=='L') l++; if(max(w,l)>=21&&abs(w-l)>=2) { cout<<w<<":"<<l<<endl; w=0; l=0; } if(wl[i]=='E') { cout<<w<<":"<<l<<endl; break; } }

}


|