0分求助!!!

P1042 [NOIP2003 普及组] 乒乓球

liyifanglan @ 2024-07-14 16:39:44

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

int main(){
    string s;
    while(1){
        char c;
        cin >> c;
        if (c == 'E') break;
        s += c;
    }
    int w, l, len = s.size();
    //11
    int x;
    if (len%11 == 0) x = len/11;
    else x = len/11+1;

    for (int i = 1; i <= x; i++){
        if (max(w, l) >= 11 && abs(w-l) >= 2){
            printf("%d:%d\n", w, l);
        }
        if (s[i] == 'W') w++;
        else l++;
    }

    cout << endl;
    //21
    w = l = 0;
    if (len%21 == 0) x = len/21;
    else x = len/21+1;

    for (int i = 1; i <= x; i++){
        if (max(w, l) >= 21 && abs(w-l) >= 2){
            printf("%d:%d\n", w, l);
        }
        if (s[i] == 'W') w++;
        else l++;
    }

}

by zsq_br @ 2024-07-14 16:42:55

题目是啥??


by liyifanglan @ 2024-07-14 16:43:44

P1042 [NOIP2003 普及组] 乒乓球


by wangruiqi36 @ 2024-07-14 17:28:06

@xd23lyfl

首先,x表示几轮比赛没有意义,因为一场比赛不一定就是进行11或21次得分,则for循环应改成

for (int i = 0/*下标从0开始*/; i < s.size()/*或len*/; i++) {
  /*先加得分,因为如果先判断一局结束,那么最后一次加完分后若往后没有得分则将不会判断一局是否结束,则会跳出循环 反例:WWWWWWWWWWWE */
    if (s[i] == 'W') w++;
    else l++;

    if (max(w, l) >= 11/*或21*/ && abs(w - l) >= 2) {
        printf("%d:%d\n", w, l);
        w = 0; l = 0;/*一局结束后清0*/
    }
}

而且,在循环结束后可能还有一些剩余的得分,也要输出(即使0:0也要输出)


by wangruiqi36 @ 2024-07-14 17:29:11

@xd23lyfl 还有,局部变量要清0


by liyifanglan @ 2024-07-14 17:49:55

谢谢!!!


by liyifanglan @ 2024-07-15 10:07:21

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

int main(){
    char c;
    string s;
    while (cin >> c){
        if (c == 'E') break;
        s += c;
    }
    int len = s.size(), w = 0, l = 0;
    //11
    for (int i = 0; i < len; i++){
        if (max(w, l) >= 11 && abs(w-l) >= 2){
            printf("%d:%d\n", w, l);
            w = l = 0;
        }
        if (s[i] == 'W') w++;
        if (s[i] == 'L') l++;
    }
    printf("%d:%d\n\n", w, l);
    w = l = 0;
    //21
    for (int i = 0; i < len; i++){
        if (max(w, l) >= 21 && abs(w-l) >= 2){
            printf("%d:%d\n", w, l);
            w = l = 0;
        }
        if (s[i] == 'W') w++;
        if (s[i] == 'L') l++;
    }
    printf("%d:%d\n", w, l);
}

@wangruiqi36 90分怎么回事


by wangruiqi36 @ 2024-07-19 22:18:04

@liyifanglan 还要先加分再判断


by liyifanglan @ 2024-07-20 14:06:21

谢谢!!!!!!


|