奇葩代码

P1957 口算练习题

luoguandy @ 2024-01-23 21:09:18

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,sum=0,num=0;
    cin>>n;
    char s;
    int a,b;
    for(int i=1;i<=n;i++)
    {
        cin>>s>>a>>b;
        int asize=0,bsize=0,numsize=0;
        if(a>=0&&a<=9) asize=1;
        if(a>=10&&a<=99) asize=2;
        if(a>=100&&a<=999) asize=3;
        if(a>=1000&&a<=9999) asize=4;
        if(b>=0&&b<=9) bsize=1;
        if(b>=10&&b<=99) bsize=2;
        if(b>=100&&b<=999) bsize=3;
        if(b>=1000&&b<=9999) bsize=4;
        if(s=='a')
        {
            num=a+b;
            if(num>=0&&num<=9) numsize=1;
            if(num>=10&&num<=99) numsize=2;
            if(num>=100&&num<=999) numsize=3;
            if(num>=1000&&num<=9999) numsize=4;
            if(num>=10000&&num<=99999) numsize=5;
            sum=2+numsize+asize+bsize;  
            cout<<a<<"+"<<b<<"="<<num<<endl<<sum<<endl;     
        }
        if(s=='b')
        {
            num=a-b;
            if(num>=0&&num<=9) numsize=1;
            if(num>=10&&num<=99) numsize=2;
            if(num>=100&&num<=999) numsize=3;
            if(num>=1000&&num<=9999) numsize=4;
            if(num<0)
            {
                if(num>=-9&&num<=-1) numsize=2;
                if(num>=-99&&num<=-10) numsize=3;
                if(num>=-999&&num<=-100) numsize=4;
                if(num>=-9999&&num<=-1000) numsize=5;
                if(num>=-99999&&num<=-10000) numsize=6;
            }
            sum=2+numsize+asize+bsize;  
            cout<<a<<"-"<<b<<"="<<num<<endl<<sum<<endl;
        }
        if(s=='c')
        {
            num=a*b;
            if(num>=0&&num<=9) numsize=1;
            if(num>=10&&num<=99) numsize=2;
            if(num>=100&&num<=999) numsize=3;
            if(num>=1000&&num<=9999) numsize=4;
            if(num>=10000&&num<=99999) numsize=5;
            if(num>=100000&&num<=999999) numsize=6;
            if(num>=1000000&&num<=9999999) numsize=7;
            if(num>=10000000&&num<=99999999) numsize=8;
            if(num>=100000000&&num<=999999999) numsize=9;
            sum=2+numsize+asize+bsize;  
            cout<<a<<"*"<<b<<"="<<num<<endl<<sum<<endl;
        }
    }
    return 0;
}

这份代码还有救吗


by pour_demain @ 2024-01-23 21:11:13


by Steve_xh @ 2024-01-23 21:16:18

@luoguandy 运算数为非负数还要考虑负数?


by luoguandy @ 2024-01-24 20:41:20

@Steve_xh 不用啊


|