P1957 HELP(样例能过,全WA)

P1957 口算练习题

Whataniceday @ 2022-07-22 23:59:18

输入:

1

a 99 999

输出:

99+999=1098

11

代码:

#include<bits/stdc++.h>
int getdigit(int a)//得到数字位数用的函数 
{
    int gd;
    for(gd=0;a!=0;gd++)
    {
        a-=a%10;
        a/=10;
    }
    return gd;
}
using namespace std;
int main()
{
    int TiShu,num1,num2,ans,tens;
    int i,j;
    char c,v;
    cin>>TiShu;//读入有几道题 
    getchar();//防止下一个读取字符出问题 
    for(i=1;i<=TiShu;i++)
    {
        c=getchar();//读取一个字符 
        cin>>num1>>num2;//输入两个数字 
        getchar();//防止下一个读取字符出问题 
        if((c=='a'||c=='b')||c=='c')//判断是不是abc 
        {
            cout<<num1;
            switch(c)
            {
                case 'a':
                    cout<<"+";
                    ans=num1+num2;
                    break;
                case 'b':
                    cout<<"-";
                    ans=num1-num2;
                    break;
                case 'c':
                    cout<<"*";
                    ans=num1*num2;
                    break;
            }
            cout<<num2<<"="<<ans<<endl;
            if(ans<0)
            cout<<getdigit(num1)+getdigit(num2)+getdigit(ans)+3<<endl;//如果是答案是负数,位数+1 
            else
            cout<<getdigit(num1)+getdigit(num2)+getdigit(ans)+2<<endl;
            v=c;//如果出现不是abc的情况,v为代替字符 
        }
        else
        {
            for(j=1,tens=1;j<=getdigit(num1);j++)
            tens*=10;
            num1+=int(c-'0')*tens;//将第一个数字还原 
            cout<<num1;
            switch(v)
            {
                case 'a':
                    cout<<"+";
                    ans=num1+num2;
                    break;
                case 'b':
                    cout<<"-";
                    ans=num1-num2;
                    break;
                case 'c':
                    cout<<"*";
                    ans=num1*num2;
                    break;
            }
            cout<<num2<<"="<<ans<<endl;
            if(ans<0)
            cout<<getdigit(num1)+getdigit(num2)+getdigit(ans)+3<<endl;//如果是答案是负数,位数+1 
            else
            cout<<getdigit(num1)+getdigit(num2)+getdigit(ans)+2<<endl;
        } 
    } 
    return 0;
}

by nikodo @ 2022-07-23 21:13:11

@Whatanicidayif大军来袭:

#include<bits/stdc++.h>
using namespace std;
int n[50],m[50],f,ans;
struct tsuanshi{
    char c;
    int aa;
    int bb;
    int len1;
    int len2;
};
void output(tsuanshi y,bool o){
    int rr1;
    switch(y.c){
        case 'a':cout<<y.aa<<"+"<<y.bb<<"="<<y.aa+y.bb<<endl;rr1=y.aa+y.bb;break;
        case 'b':cout<<y.aa<<"-"<<y.bb<<"="<<y.aa-y.bb<<endl;rr1=y.aa-y.bb;break;
        case 'c':cout<<y.aa<<"*"<<y.bb<<"="<<y.aa*y.bb<<endl;rr1=y.aa*y.bb;break;
    }
    int ans=0,ans1=0,ans2=0;
    if(y.aa<0)
    ans++;
    if(y.bb<0)
    ans1++;
    if(rr1<0)
    ans2++;
    if(y.aa==0)
    ans=1;
    if(y.bb==0)
    ans1=1;
    if(rr1==0)
    ans2=1;
    if(!rr1==0)
        while(rr1!=0){
            ans2++;
            rr1/=10;
        }
    if(!y.bb==0)
        while(y.bb!=0){
            ans1++;
            y.bb/=10;
        }
    if(!y.aa==0)
        while(y.aa!=0){
            ans++;
            y.aa/=10;
        }
    if(o==false)
    cout<<ans+1+ans1+1+ans2<<endl;
    else
    cout<<ans+1+ans1+1+ans2;    
}
int main(){
    cin>>f;
    tsuanshi s[50];
    string a,b;
    for(int i=0;i<f;i++){
        cin>>a;
        if(a.length()==1){
            s[i].c=a[0];
            cin>>a>>b;
            s[i].aa=atoi(a.c_str());
            s[i].bb=atoi(b.c_str());
            s[i].len1=a.length();
            s[i].len2=b.length();
            continue;
        }else{
            s[i].c=s[i-1].c;
            cin>>b;
            s[i].aa=atoi(a.c_str());
            s[i].bb=atoi(b.c_str());
            s[i].len1=a.length();
            s[i].len2=b.length();
        }
    }
    for(int i=0;i<f-1;i++)
    output(s[i],false);
    output(s[f-1],true);
    return 0;
}

|