P1957 口算练习题

4041nofoundGeoge @ 2024-04-19 15:09:59

#include <bits/stdc++.h>
using namespace std;
int wei(int x)
{
    int cnt=0;
    int q=x;
    while(q!=0)
    {
        cnt++;
        q/=10;
    }
    return cnt;
}
int szi(string x)
{
    int sum=0;
    for(int i=0;i<=x.size()-1;i++)
    {
        sum=sum*10+x[i]-'0';
    }
    return sum;
}
int main()
{
    int n;
    cin>>n;
    int last;
    for(int i=1;i<=n;i++)
    {

        int ans=0;
        int len=0;
        string m;
        string a,b;
        cin>>m>>a;
        if(m=="a")
        {
            last=1;
            cin>>b;
            ans=szi(a)+szi(b);
            len=a.size()+b.size()+2+wei(ans);
            printf("%s+%s=%d\n%d\n",a.c_str(),b.c_str(),ans,len);
        }
        else if(m=="b")
        {
            last=2;
            cin>>b;
            ans=szi(a)-szi(b);
            len=a.size()+b.size()+2+wei(ans); 
            printf("%s-%s=%d\n%d\n",a.c_str(),b.c_str(),ans,len);
        }
        else if(m=="c")
        {
            last=3;
            cin>>b;
            ans=szi(a)*szi(b);
            len=a.size()+b.size()+2+wei(ans); 
            printf("%s*%s=%d\n%d\n",a.c_str(),b.c_str(),ans,len);
        } 
        else
        {
            b=a;
            a=m;
            if(last==1)
            {
                ans=szi(a)+szi(b);
                len=a.size()+b.size()+2+wei(ans);
                printf("%s+%s=%d\n%d\n",a.c_str(),b.c_str(),ans,len);
            }
            else if(last==2)
            {
                ans=szi(a)-szi(b);
                len=a.size()+b.size()+2+wei(ans);
                printf("%s-%s=%d\n%d\n",a.c_str(),b.c_str(),ans,len);
            }
            else if(last==3)
            {
                ans=szi(a)*szi(b);
                len=a.size()+b.size()+2+wei(ans);
                printf("%s*%s=%d\n%d\n",a.c_str(),b.c_str(),ans,len);
            }

        }
    }
    return 0;
}

by Charles_with_wkc @ 2024-04-28 21:43:54

@4041nofoundGeoge 你没有考虑特殊情况


by Charles_with_wkc @ 2024-04-28 21:44:37

@4041nofoundGeoge This is AC Code

#include<bits/stdc++.h>
using namespace std;
int n,x,y,xy;
char a[25];
char k;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        int sum=2;
        cin>>a;
        if(isalpha(a[0])){
            k=a[0];
            cin>>x>>y;
        }
        else{
            x=atoi(a);
            cin>>y;
        }
        if(k=='a'){
            cout<<x<<"+"<<y<<"="<<x+y<<endl;
            xy=x+y;
        }
        else if(k=='b'){
            cout<<x<<"-"<<y<<"="<<x-y<<endl;
            xy=x-y;
        }
        else{
            cout<<x<<"*"<<y<<"="<<x*y<<endl;
            xy=x*y;
        }
        if(xy<0){
            sum++;
        }
        if(x<0){
            sum++;
        }
        if(y<0){
            sum++;
        }
        if(xy==0){
            sum++;
        }
        if(x==0){
            sum++;
        }
        if(y==0){
            sum++;
        }
        while(xy!=0){
            xy/=10;
            sum++;
        }
        while(x!=0){
            x/=10;
            sum++;
        }
        while(y!=0){
            y/=10;
            sum++;
        }
        cout<<sum<<endl;
    }
    return 0;
}

求关注


by 4041nofoundGeoge @ 2024-04-29 17:09:54


|