只得了50分,求助

P1957 口算练习题

Kazeno_Akina @ 2022-12-02 13:44:36

#include <bits/stdc++.h>
using namespace std;
char ord,last_ord;
int m,n,ans;
int I;
int Length(int x)//求长度
{
    bool down_zero=0;
    int i;
    if(x<0)
    {
        x=-x;
        down_zero=1;
    }
    for(i=1;true;i++)
    {
        if(x<=pow(10,i)) break;
    }
    if(down_zero) return i+1;
    return i;
}
void Plus(int x,int y)//加法计算
{
    ans=x+y;
    cout << x << "+" << y << "=" << ans << endl;
    cout << Length(x)+Length(y)+Length(ans)+2 << endl;
    last_ord=ord;
}
void Subtr(int x,int y)//减法计算
{
    ans=x-y;
    cout << x << "-" << y << "=" << ans << endl;
    cout << Length(x)+Length(y)+Length(ans)+2 << endl;
    last_ord=ord;
}
void Multi(int x,int y)//乘法计算
{
    ans=x*y;
    cout << x << "*" << y << "=" << ans << endl;
    cout << Length(x)+Length(y)+Length(ans)+2 << endl;
    last_ord=ord;
}
int main()
{
    cin >> I;
    while(I--)
    {
        cin >> ord;
        if(ord<='c'&&ord>='a')
        {
            cin >> m >> n;
            if(ord=='a') Plus(m,n);
            else if(ord=='b') Subtr(m,n);
            else Multi(m,n);
        }
        else
        {
            cin >> m >> n;
            m+=((int(ord)-48)*pow(10,Length(m)));
            if(last_ord=='a') Plus(m,n);
            else if(last_ord=='b') Subtr(m,n);
            else Multi(m,n);
        }
    }
    return 0;
}

by ChrisWangZi @ 2022-12-02 14:12:30

求长度写得有问题。

for中的判断应为:

if(x<pow(10,i)) break;

否则10的整数次幂是错的。


|