为什么测试数据没问题,提交后全wa

B2120 单词的长度

adtom @ 2023-03-01 18:55:54

//#include<iostream>
//#include<cmath>
#include<stdio.h>
//#include<string.h>
using namespace std;
int main(){
    int temp=0,j=0,flag=1,a[300]={0};
    char c[1000]={};
    fgets(c,1000,stdin);  
    for(int i=0;i<strlen(c)-1;i++){
        if(c[i]!=32){
            flag=1;
            temp++;
        }else{
            if(flag==1){
                a[j++]=temp;
                flag=0;
                temp=0;
            }
            continue;
        }
        if(i==strlen(c)-2)
            a[j++]=temp;
    }
    for(int i=0;i<j;i++){
        if(i<j-1)
            cout<<a[i]<<',';
        else 
            cout<<a[i];
    }
}

by CCX_Ug @ 2023-03-01 19:18:57

直接

scanf("%s", c);
printf("%d", strlen(c));
while (scanf("%s", c) == 1)
    printf(",%d", strlen(c));
printf("\n");

就行了 建议把字符数组开到1010防止数据太咕咕咕


|