70分蒟蒻求助

P1957 口算练习题

WsW_ @ 2020-11-30 19:14:25

#include<bits/stdc++.h>
using namespace std;
int n,a,b;
char x,x1;
int cd(int s){
    int sum=0;
    if(s<0)sum++;
    s=abs(s);
    while(s>0){
        s/=10;
        sum++;
    }
    return sum;
}
int main(){
    scanf("%d",&n);
    while(n--){
        cin>>x1;
        if(x1=='a'||x1=='b'||x1=='c'){
            scanf("%d%d",&a,&b);
            x=x1;
        }
        else{
            scanf("%d%d",&a,&b);
            if(x1!='-')a+=pow(10,cd(a))*(int(x1)-'0');
            else a=-a;
        }
        printf("%d",a);
        if(x=='a')printf("+");
        if(x=='b')printf("-");
        if(x=='c')printf("*");
        printf("%d",b);
        if(x=='a')printf("=%d\n%d\n",a+b,2+cd(a)+cd(b)+cd(a+b));
        if(x=='b')printf("=%d\n%d\n",a-b,2+cd(a)+cd(b)+cd(a-b));
        if(x=='c')printf("=%d\n%d\n",a*b,2+cd(a)+cd(b)+cd(a*b));
    }
    return 0;
} 

by suyue1098765432 @ 2020-11-30 19:31:34

@骚皮 printf调用的cd()函数如果传入0的话会返回0位,但实际上0应该是一位数,而a+=pow(10,cd(a))*(int(x1)-'0')调用的cd()从式子来看应该返回0。


by WsW_ @ 2020-11-30 20:15:51

@suyue1098765432 现在90分了

#include<bits/stdc++.h>
using namespace std;
int n,a,b;
char x,x1;
int cd(int s){
    if(s==0)return 1;
    int sum=0;
    if(s<0)sum++;
    s=abs(s);
    while(s>0){
        s/=10;
        sum++;
    }
    return sum;
}
int main(){
    scanf("%d",&n);
    while(n--){
        cin>>x1;
        if(x1=='a'||x1=='b'||x1=='c'){
            scanf("%d%d",&a,&b);
            x=x1;
        }
        else{
            scanf("%d%d",&a,&b);
            if(x1!='-')a+=pow(10,cd(a))*(int(x1)-'0');
            else a=-a;
        }
        printf("%d",a);
        if(x=='a')printf("+");
        if(x=='b')printf("-");
        if(x=='c')printf("*");
        printf("%d",b);
        if(x=='a')printf("=%d\n%d\n",a+b,2+cd(a)+cd(b)+cd(a+b));
        if(x=='b')printf("=%d\n%d\n",a-b,2+cd(a)+cd(b)+cd(a-b));
        if(x=='c')printf("=%d\n%d\n",a*b,2+cd(a)+cd(b)+cd(a*b));
    }
    return 0;
}

by suyue1098765432 @ 2020-11-30 20:19:58

if(x1!='-')a+=pow(10,cd(a))*(int(x1)-'0'); 这一句话,如果输入为0 10,scanf()会出问题

  1. 他会直接把10赋值给a,然后多读一个数

  2. 如果x1=='0',a==0赋值也有问题


by suyue1098765432 @ 2020-11-30 20:20:22

建议用快读


by suyue1098765432 @ 2020-11-30 20:20:31

@骚皮


by WsW_ @ 2020-11-30 21:39:33

@suyue1098765432

#include<bits/stdc++.h>
using namespace std;
int n,a,b;
char x,x1,x2;
int cd(int s){
    if(s==0)return 1;
    int sum=0;
    if(s<0)sum++;
    s=abs(s);
    while(s>0){
        s/=10;
        sum++;
    }
    return sum;
}
int main(){
    scanf("%d",&n);
    while(n--){
        cin>>x1;
        if(x1=='a'||x1=='b'||x1=='c'){
            scanf("%d%d",&a,&b);
            x=x1;
        }
        else{
            scanf("%c",&x2);
            if(x2!=' '){
                scanf("%d%d",&a,&b);
                if(x1!='-')a+=pow(10,cd(a)+1)*(int(x1)-'0')+pow(10,cd(a))*(int(x2)-'0');
                else a=-a;
            }
            else{
                scanf("%d",&b);
                a=x1-'0';
            }
        }
        printf("%d",a);
        if(x=='a')printf("+");
        if(x=='b')printf("-");
        if(x=='c')printf("*");
        printf("%d",b);
        if(x=='a')printf("=%d\n%d\n",a+b,2+cd(a)+cd(b)+cd(a+b));
        if(x=='b')printf("=%d\n%d\n",a-b,2+cd(a)+cd(b)+cd(a-b));
        if(x=='c')printf("=%d\n%d\n",a*b,2+cd(a)+cd(b)+cd(a*b));
    }
    return 0;
}

还是错了


by suyue1098765432 @ 2020-11-30 21:41:31

@骚皮 那如果a是两位数scanf又会出问题


by WsW_ @ 2020-11-30 21:47:43

?我加了特判啊@suyue1098765432


by suyue1098765432 @ 2020-12-01 08:17:58

@骚皮 else a=-a;这里你没改啊


|