求调代码,悬赏关注,50分

P1957 口算练习题

lcx2011 @ 2024-01-30 19:23:56

#include <bits/stdc++.h>
using namespace std;
string gjj(string n,string m){
    int la=n.length();
    int lb=m.length();
    int z[100000]={0};
    string x="";
    int a[100000],b[100000];
    for(int i=la-1;i>=0;i--){
        a[la-i]=n[i]-'0';
    }
    for(int i=lb-1;i>=0;i--){
        b[lb-i]=m[i]-'0';
    }
    for(int i=1;i<=max(la,lb);i++){
        z[i-1]+=a[i]+b[i];
        z[i]=z[i-1]/10;
        z[i-1]%=10;
    }
    int ls=max(la,lb);
    if(z[ls]!=0) ls++;
    for(int i=ls-1;i>=0;i--){
        x+=z[i]+'0';
    }
    return x;
}
string gjc(string n,string m){
    int la=n.length();
    int lb=m.length();
    int a[100000],b[100000],c[100000];
    for(int i=1;i<=la;i++) a[i]=n[la-i]-'0';
    for(int i=1;i<=lb;i++) b[i]=m[lb-i]-'0';
    for(int i=1;i<=lb;i++)
    for(int j=1;j<=la;j++)
    c[i+j-1]+=a[j]*b[i];
    for(int i=1;i<la+lb;i++)
    if(c[i]>9)
    {
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    int l=la+lb;
    while(c[l]==0&&l>1)l--;
    string x="";
    for(int i=l;i>=1;i--)x+=c[i]+'0';
    return x;
}
bool pd;
string gjji(string a,string b){
    int na[100000], nb[100000], ans[100000]; 
    if((a < b && a.size() == b.size()) || a.size() < b.size())
    {
        swap(a, b);
        pd = true;
    }
    for(int i = a.size(); i > 0; i --)na[i] = a[a.size() - i] - '0';
    for(int i = b.size(); i > 0; i --)nb[i] = b[b.size() - i] - '0';
    int maxl = max(a.size(), b.size());
    for(int i = 1; i <= maxl; i ++)
    {
        if(na[i] < nb[i])
        {
            na[i + 1] --;
            na[i] += 10;
        }
        ans[i] = na[i] - nb[i];
    }

    while(ans[maxl] == 0)maxl --;
    string x="";
    if(pd == true)x+='-';

    for(int i = maxl; i > 0; i --)x+='0'+ans[i];
    if(maxl < 1)x+="0";
    return x;
}
string a,x,y,t;
int main(){
    int n;
    cin>>n;
    while(n--){
        cin>>a;
        string ans;
        if(a=="a"){
            t=a;
            cin>>x>>y;
            ans=gjj(x,y);
            cout<<x<<'+'<<y<<'='<<ans<<endl;
            cout<<ans.length()+x.length()+y.length()+2<<endl;
        }
        else if(a=="b"){
            t=a;
            cin>>x>>y;
            ans=gjji(x,y);
            cout<<x<<'-'<<y<<'='<<ans<<endl;
            cout<<ans.length()+x.length()+y.length()+2<<endl;
        }
        else if(a=="c"){
            t=a;
            cin>>x>>y;
            ans=gjc(x,y);
            cout<<x<<'*'<<y<<'='<<ans<<endl;
            cout<<ans.length()+x.length()+y.length()+2<<endl;
        }
        else {
            cin>>y;
            if(t=="a"){
            //  cout<<111111;
                ans=gjj(a,y);
                cout<<a<<'+'<<y<<'='<<ans<<endl;
                cout<<ans.length()+a.length()+y.length()+2<<endl;
            }
            else if(t=="b"){
                ans=gjji(a,y);
                cout<<a<<'-'<<y<<'='<<ans<<endl;
                cout<<ans.length()+a.length()+y.length()+2<<endl;
            }
            else if(t=="c"){
                ans=gjc(a,y);
                cout<<a<<'*'<<y<<'='<<ans<<endl;
                cout<<ans.length()+a.length()+y.length()+2<<endl;
            }
        }
    }
    return 0;
}

样例全过,提交50


|