找一个比我暴力的!

P1957 口算练习题

Double_U_Q @ 2024-10-28 22:35:02

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int main()
{
    int n;
    cin>>n;
    char c;
    int a,b;
    cin>>c>>a>>b;
    char r = c;
    if(c=='a') {
        cout<<a<<"+"<<b<<"="<<a+b<<endl;
        int sum = 0;
        if(a==0) sum+=1;
        else sum+=(int)(1+log10(a));
        if(b==0) sum+=1;
        else sum+=(int)(1+log10(b));
        if(a+b==0) sum+=1;
        else sum+=(int)(1+log10(a+b));
        cout<<sum+2<<endl;
    }
    if(c=='b') {
        cout<<a<<"-"<<b<<"="<<a-b<<endl;
        int sum = 0;
        if(a==0) sum+=1;
        else sum+=(int)(1+log10(a));
        if(b==0) sum+=1;
        else sum+=(int)(1+log10(b));
        if(a-b<0) {
            sum+=(int)(1+log10(b-a));
            cout<<sum+3<<endl;
        }
        else if(a-b==0) {
            sum+=1;
            cout<<sum+2<<endl;
        }
        else cout<<sum+2+(int)(1+log10(a-b))<<endl;
    }
    if(c=='c') {
        cout<<a<<"*"<<b<<"="<<a*b<<endl;
        int sum = 0;
        if(a==0) sum+=1;
        else sum+=(int)(1+log10(a));
        if(b==0) sum+=1;
        else sum+=(int)(1+log10(b));
        if(a*b==0) sum+=1;
        else sum+=(int)(1+log10(a*b));
        cout<<sum+2<<endl;
    }
    n-=1;
    for(int i = 0; i<n; i++){
        char t;
        string m;
        cin>>m;
        t = m[0];
        if(t>='a'&&t<='c')
        {
            r = t;
            int x,y;
            cin>>x>>y;
            if(t=='a') {
                cout<<x<<"+"<<y<<"="<<y+x<<endl;
                int sum = 0;
                if(y==0) sum+=1;
                else sum+=(int)(1+log10(y));
                if(x==0) sum+=1;
                else sum+=(int)(1+log10(x));
                if(y+x==0) sum+=1;
                else sum+=(int)(1+log10(y+x));
                cout<<sum+2<<endl;
            }
            if(t=='b') {
                cout<<x<<"-"<<y<<"="<<x-y<<endl;
                int sum = 0;
                if(y==0) sum+=1;
                else sum+=(int)(1+log10(y));
                if(x==0) sum+=1;
                else sum+=(int)(1+log10(x));
                if(x-y<0) {
                    sum+=(int)(1+log10(y-x));
                    cout<<sum+3<<endl;
                }
                else if(x-y==0) {
                    sum+=1;
                    cout<<sum+2<<endl;
                }
                else cout<<sum+2+(int)(1+log10(x-y))<<endl;
            }
            if(t=='c') {
                cout<<x<<"*"<<y<<"="<<y*x<<endl;
                int sum = 0;
                if(y==0) sum+=1;
                else sum+=(int)(1+log10(y));
                if(x==0) sum+=1;
                else sum+=(int)(1+log10(x));
                if(y*x==0) sum+=1;
                else sum+=(int)(1+log10(y*x));
                cout<<sum+2<<endl;
            }
        }
        else
        {
            int x;
            cin>>x;
            int y = stoi(m);
            if(r=='a') {
                cout<<y<<"+"<<x<<"="<<y+x<<endl;
                int sum = 0;
                if(y==0) sum+=1;
                else sum+=(int)(1+log10(y));
                if(x==0) sum+=1;
                else sum+=(int)(1+log10(x));
                if(y+x==0) sum+=1;
                else sum+=(int)(1+log10(y+x));
                cout<<sum+2<<endl;
            }
            if(r=='b') {
                cout<<y<<"-"<<x<<"="<<y-x<<endl;
                int sum = 0;
                if(y==0) sum+=1;
                else sum+=(int)(1+log10(y));
                if(x==0) sum+=1;
                else sum+=(int)(1+log10(x));
                if(y-x<0) {
                    sum+=(int)(1+log10(x-y));
                    cout<<sum+3<<endl;
                }
                else if(y-x==0) {
                    sum+=1;
                    cout<<sum+2<<endl;
                }
                else cout<<sum+2+(int)(1+log10(y-x))<<endl;
            }
            if(r=='c') 
            {
                cout<<y<<"*"<<x<<"="<<y*x<<endl;
                int sum = 0;
                if(y==0) sum+=1;
                else sum+=(int)(1+log10(y));
                if(x==0) sum+=1;
                else sum+=(int)(1+log10(x));
                if(y*x==0) sum+=1;
                else sum+=(int)(1+log10(y*x));
                cout<<sum+2<<endl;
            }
        }
    }
}

|