蒟蒻求助

P1042 [NOIP2003 普及组] 乒乓球

boxadeny @ 2024-09-24 15:05:19

请问为什么第5行换成string就wa又re,用char[]就ac?

#include<iostream>
#include<cmath>
using namespace std;
int cnt = 0;
char str[90000];
void print(int x) {
    int a = 0,b = 0;
    for(int i = 0;i < cnt;i++) {
        if(str[i] == 'W')a++;
        if(str[i] == 'L')b++;
        if((a>=x||b>=x)&&abs(a-b)>=2) {
            cout<<a<<":"<<b<<endl;
            a = 0;b = 0;
        }
    }
    cout<<a<<":"<<b<<endl;
}
int main() {
    char ch;
    while(cin>>ch&&ch!='E') {
        if(ch=='W'||ch=='L')str[cnt++] = ch;
    }
    print(11);
    cout<<endl;
    print(21);
    return 0;
}

by yinfelix @ 2024-09-24 15:19:54

@boxadeny

#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int cnt = 0;
string str;
void print(int x) {
    int a = 0,b = 0;
    for(int i = 0;i < cnt;i++) {
        if(str[i] == 'W')a++;
        if(str[i] == 'L')b++;
        if((a>=x||b>=x)&&abs(a-b)>=2) {
            cout<<a<<":"<<b<<endl;
            a = 0;b = 0;
        }
    }
    cout<<a<<":"<<b<<endl;
}
int main() {
    char ch;
    while(cin>>ch&&ch!='E') {
        if(ch=='W'||ch=='L') str = str + ch,cnt++;//要这样加
        //cout<<str<<endl; 你可以试试用你之前的代码输出str

    }
    print(11);
    cout<<endl;
    print(21);
    return 0;
}

by boxadeny @ 2024-09-25 14:06:55

@yinfelix 谢谢佬


|