60分求助,回复有关

P1042 [NOIP2003 普及组] 乒乓球

lce11451410086 @ 2024-12-05 21:00:43

#include<bits/stdc++.h>
using namespace std;
char c[100000];
void bs(int n){
    int a=0,b=0,len=0;
    for(int i=0;i<strlen(c)-1;i++){
        if(c[i]=='W'){
            a++;
            len++;
        }else if(c[i]=='L'){
            b++;
            len++;
        }
        if(abs(a-b)>=2&&len>=n){
            printf("%d:%d\n",a,b);
            a=b=0;
            len=0;
        }
    }
    printf("%d:%d",a,b);
}
int main(){
    for(int i=0;;i++){
        scanf("%c",&c[i]);
        if(c[i]=='E'){
            break;
        }
    }
    bs(11);
    printf("\n");
    printf("\n");
    bs(21);
    return 0;
}

by cncnk205634848 @ 2024-12-05 21:27:47

14行不是len>=n,是max(a,b)>=n; 比如a=9,b=2,它会输出9:2 ac代码:(别忘了关注)

#include<bits/stdc++.h>
using namespace std;
char c[100000];
void bs(int n){
    int a=0,b=0,len=0;
    for(int i=0;i<strlen(c)-1;i++){
        if(c[i]=='W'){
            a++;
            len++;
        }else if(c[i]=='L'){
            b++;
            len++;
        }
        if(abs(a-b)>=2&&max(a,b)>=n){    
            printf("%d:%d\n",a,b);
            a=b=0;
            len=0;
        }
    }
    printf("%d:%d",a,b);
}
int main(){
    for(int i=0;;i++){
        scanf("%c",&c[i]);
        if(c[i]=='E'){
            break;
        }
    }
    bs(11);
    printf("\n");
    printf("\n");
    bs(21);
    return 0;
}

by cncnk205634848 @ 2024-12-05 21:29:32

其实len不用设...


by ycyjx @ 2024-12-07 16:17:50

#include<bits/stdc++.h>
using namespace std;
char s;
int a[1000001]={};
int main(){
    int n=1,w=0,l=0;
    while (cin>>s&&s!='E'){
        if (s=='W')a[n]=1;
        else if (s=='L')a[n]=2;
        n++;
    }
    for (int i=1;i<=n;i++){
        if (a[i]==1)w++;
        if (a[i]==2)l++;
        if (a[i]==0){
            cout<<w<<":"<<l<<endl<<endl;
            break;
        }
        if (abs(w-l)>=2){
            if (w>=11||l>=11){
                cout<<w<<":"<<l<<endl;
                w=0;
                l=0;
            }
        }
    }
    w=0;
    l=0;
    for (int i=1;i<=n;i++){
        if (a[i]==1)w++;
        if (a[i]==2)l++;
        if (a[i]==0){
            cout<<w<<":"<<l<<endl<<endl;
            break;
        }
        if (abs(w-l)>=2){
            if (w>=21||l>=21){
                cout<<w<<":"<<l<<endl;
                w=0;
                l=0;
            }
        }
    }
    return 0;
}

by ycyjx @ 2024-12-07 16:19:29

也可以用函数 (不用写那么多)


by lce11451410086 @ 2024-12-08 13:26:18

@cncnk205634848@ycyjx 谢谢


|