求助,哪位大佬帮忙看看错哪了

P1957 口算练习题

lwb2008 @ 2020-08-18 17:03:30

#include<bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
using namespace std;
int n,s1,s2,ans1,ans2,ans3,ans4;
char x,s;
int node(int ss1,int ss2)
{
    ans1=ans2=ans3=ans4=1;
    int t1,t2,t3,ss3;
    t1=ss1;
    t2=ss2;
    while (t1!=0)
    {
        t1/=10;
        ans1++;
    }
    while (t2!=0)
    {
        t2/=10;
        ans2++;
    }
    ss3=ans1+ans2;
    t3=ans3;
    while (t3!=0)
    {
        t3/=10;
        ans3++;
    }
    ans4=ans1+ans2+ans3+3;
    return ans4;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        cin>>x;
        if (x=='a')
        {
            cin>>s1>>s2;
            cout<<s1<<"+"<<s2<<"="<<s1+s2<<endl<<node(s1,s2)<<endl;
            s='a';
        }
        if (x=='b')
        {
            cin>>s1>>s2;
            cout<<s1<<"-"<<s2<<"="<<s1-s2<<endl<<node(s1,s2)<<endl;
            s='b';
        }
        if (x=='c')
        {
           cin>>s1>>s2;
           cout<<s1<<"*"<<s2<<"="<<s1*s2<<endl<<node(s1,s2)<<endl;
           s='c';
        }
        else
        {
            if (s=='a')
            {
                cin>>s1>>s2;
                cout<<s1<<"+"<<s2<<"="<<s1+s2<<endl<<node(s1,s2)<<endl;
                s='k';
            }
            if (s=='b')
            {
                cin>>s1>>s2;
                cout<<s1<<"-"<<s2<<"="<<s1-s2<<endl<<node(s1,s2)<<endl;
                s='k';
            }
            if (s=='c')
            {
                cin>>s1>>s2;
                cout<<s1<<"*"<<s2<<"="<<s1*s2<<endl<<node(s1,s2)<<endl;
                s='k';
            }
        }
    }
    //system("pause");
    return 0;
}

by J26jy @ 2020-08-18 17:15:42

可以这样做:

#include<bits/stdc++.h>
using namespace std;
int n,a,b;
string c;
char l_c;
int len(int number)
{
    int x=0;
    if(number==0)
        return 1;
    if(number>0)
        while(number){
            number/=10;
            ++x;
        }
    if(number<0){
        x=1;
        number=-number;
        while(number){
            number/=10;
            ++x;
        }
    }
    return x;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>c;
        int x;
        switch(c[0]){
            case 'a': cin>>a>>b;
                printf("%d+%d=%d\n",a,b,a+b);x=len(a+b)+2;
                l_c=c[0];break;
            case 'b': cin>>a>>b;
                printf("%d-%d=%d\n",a,b,a-b);x=len(a-b)+2;
                l_c=c[0];break;
            case 'c': cin>>a>>b;
                printf("%d*%d=%d\n",a,b,a*b);x=len(a*b)+2;
                l_c=c[0];break;
            default:
                cin>>b;
                a=0;
                int l=c.size();
                for(int i=0;i<l;++i)
                    a=a*10+c[i]-'0';
                switch(l_c) {
                    case 'a':
                        printf("%d+%d=%d\n",a,b,a+b);x=len(a+b)+2;break;
                    case 'b':
                        printf("%d-%d=%d\n",a,b,a-b);x=len(a-b)+2;break;
                    case 'c':
                        printf("%d*%d=%d\n",a,b,a*b);x=len(a*b)+2;break;
                    default:break;
                }   
                break;
        }
        x=len(a)+len(b)+x;
        printf("%d\n",x);
    }
    return 0;
}

by J26jy @ 2020-08-18 17:16:40

求关注!!!


|