求助,不知道哪里错了

P1957 口算练习题

黄舀啊 @ 2021-09-17 19:01:04

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

|