最后一个数据无法ac,求问还有哪里没处理好
查看原帖
最后一个数据无法ac,求问还有哪里没处理好
1257000
_AshGrey_楼主2024/9/10 20:38
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <cmath>

int main() {

    // begin input

    std::vector<std::string> score;
    std::string input;
    while (std::cin >> input) {
        score.push_back(input);
        if (input.find('E') != std::string::npos) {
            break;
        }
    }

    // end input
    
    std::vector<std::pair<unsigned int, unsigned int>> rate_11, rate_21;
    unsigned int count_11_w = 0, count_11_l = 0,
                 count_21_w = 0, count_21_l = 0;

    // begin calculate rate

    for (const auto& line : score) {
        for (const auto ch : line) {
            if (ch == 'W') {
                ++count_11_w;
                ++count_21_w;
            } else if (ch == 'L') {
                ++count_11_l;
                ++count_21_l;
            } else if (ch == 'E') {
                break;
            }
            if ((count_11_w >= 11 || count_11_l >= 11)
             && std::abs(static_cast<int>(count_11_w) -  
                         static_cast<int>(count_11_l)) >= 2) {
                rate_11.emplace_back(count_11_w, count_11_l);
                count_11_w = count_11_l = 0;
            }
            if ((count_21_w >= 21 || count_21_l >= 21)
             && std::abs(static_cast<int>(count_21_w) -
                         static_cast<int>(count_21_l)) >= 2) {
                rate_21.emplace_back(count_21_w, count_21_l);
                count_21_w = count_21_l = 0;
            }
            
        }
    }

    // end calculate rate : 1.notice that count_11(21)_w(l) may not be 0,
    //                    :   so we need to deal with the last non-zero
    //                    :   score rate
    //                    : 2.notice that count_11(21)_w(l) are `unsigned
    //                    :   int`, if we want to use it to calculate `abs`, 
    //                    :   we must use `static_cast<int>`

    // begin calculate last non-zero score rate

    if (count_11_w > 0 || count_11_l > 0) {
        rate_11.emplace_back(count_11_w, count_11_l);
    }
    if (rate_11.empty()) {
        rate_11.emplace_back(0, 0);
    }
    if (count_21_w > 0 || count_21_l > 0) {
        rate_21.emplace_back(count_21_w, count_21_l);
    }
    if (rate_21.empty()) {
        rate_21.emplace_back(0, 0);
    }

    // end calculate last non-zero score rate

    // begin output

    std::string output_11 = "", output_21 = "";
    for (const auto& rate : rate_11) {
        output_11 += std::to_string(rate.first)
                  +  ":"
                  +  std::to_string(rate.second)
                  +  "\n";
    }
    for (const auto& rate : rate_21) {
        output_21 += std::to_string(rate.first)
                  +  ":"
                  +  std::to_string(rate.second)
                  +  "\n";
    }
    std::cout << output_11 << "\n" << output_21;

    // end output

    return 0;
}
2024/9/10 20:38
加载中...