求大佬解惑,全WA,但是本地devc++跑样例和第一个WA都一样

P1957 口算练习题

hehebe @ 2024-11-23 23:11:51

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int change(char str[]){
    char *p;
    int l,n=0;
    p=str;
    l=strlen(str);
    while(*p!='\0'){
        switch(*p){
            case '0':n=n*10+0;break;
            case '1':n=n*10+1;break;
            case '2':n=n*10+2;break;
            case '3':n=n*10+3;break;
            case '4':n=n*10+4;break;
            case '5':n=n*10+5;break;
            case '6':n=n*10+6;break;
            case '7':n=n*10+7;break;
            case '8':n=n*10+8;break;
            case '9':n=n*10+9;break;
        }
        p++;
    }
    return n;
}
int length(int k){
    int i;
    for(k;k>0;k=k/10){
        i++;
    }
    return i;
}
int main(){
    char st1[6],st2[6];
    int flag,n,i,j,k,len;
    scanf("%d",&n);
    for(n;n>0;n--){
        scanf("%s",st1);
        if(st1[0]=='a'){
            flag=1;
            scanf("%s %s",st1,st2);
        }else
        if(st1[0]=='b'){
            flag=2;
            scanf("%s %s",st1,st2);
        }else
        if(st1[0]=='c'){
            flag=3;
            scanf("%s %s",st1,st2);
        }else
        scanf("%s",st2);
        i=change(st1);
        j=change(st2);
        if(flag==1){
            k=i+j;
            len=length(k)+strlen(st1)+strlen(st2)+2;
            printf("%d+%d=%d\n%d",i,j,k,len);
        }else
        if(flag==2){
            k=i-j;
            if(k>0){
                len=length(k)+strlen(st1)+strlen(st2)+2;
                printf("%d-%d=%d\n%d",i,j,k,len);
            }else{
                k=abs(k);
                len=length(k)+strlen(st1)+strlen(st2)+3;
                printf("%d-%d=-%d\n%d",i,j,k,len);
            }
        }
        if(flag==3){
            k=i*j;
            len=length(k)+strlen(st1)+strlen(st2)+2;
            printf("%d*%d=%d\n%d",i,j,k,len);
        }
        if(n>1){
        printf("\n");
        }
    }
    return 0;
}

by pjh0625 @ 2024-11-30 10:11:33

@hehebe

#include<bits/stdc++.h>
using namespace std;
int n,x,y;
char ss[20],ans[100],k;
int main() {
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {
        scanf("%s",&ss);
        if(isalpha(ss[0])) {
            k=ss[0];
            scanf("%d%d",&x,&y);
        } else {
            x=atoi(ss);
            scanf("%d",&y);
        }
        if(k=='a')
            sprintf(ans,"%d+%d=%d",x,y,x+y);
        else if(k=='b')
            sprintf(ans,"%d-%d=%d",x,y,x-y);
        else
            sprintf(ans,"%d*%d=%d",x,y,x*y);
        printf("%s\n%d\n",ans,strlen(ans));
    }
    return 0;
}

|