求助,麻烦大佬救救孩子,样例测试能过,但是一直WA

P1957 口算练习题

MatterMu @ 2023-02-05 18:30:51


#include<stdio.h>
#include<string.h>
int main()
{
    char a,arr[100000],ans[100005];
    int n,b,c,i=0;
    scanf("%d",&n);
    getchar();
    for(i=1;i<=n;i++)
    {
        scanf("%[^\n]",arr);
        if(arr[0]>='a'&&arr[0]<='c')
        {
            sscanf(arr,"%c%d%d",&a,&b,&c);
        }
        else
        {
            sscanf(arr,"%d%d",&b,&c);
        }
        switch(a)
        {
            case 'a':
                sprintf(ans,"%d+%d=%d",b,c,b+c);
                break;
            case 'b':
                sprintf(ans,"%d-%d=%d",b,c,b-c);
                break;
            case 'c':
                sprintf(ans,"%d*%d=%d",b,c,b*c);
                break;
        }
        printf("%s\n%d",ans,strlen(ans));
        getchar();
        memset(arr,'\0',sizeof(arr)); 
        memset(ans,'\0',sizeof(ans)); 
    }
    return 0;
}

by AlexFad @ 2023-04-28 21:06:52

改成这样就行了

scanf("%*[\r\n]%[^\n]",s);

|