萌新求助!!!不知道怎么调整输出,样例外表上能过QAQ

P1957 口算练习题

Slience @ 2020-12-23 23:35:04

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
char op;
int n;
int len(int num)
{
    int ans = 0;
    if(num < 0) {
        ans++;
        num = abs(num);
    }
    while(num)
    {
        num/=10;
        ans++;
    }
    return ans;
}
void readin()
{
    char a;
    a = getchar();
    while(a == ' '||a =='\n') a = getchar();
    long long x,y;
    if(a >= 'a' && a <= 'c') op = a;
    else cin.putback(a);
    cin >> x >> y;
    if(op == 'a') cout << x << '+' << y << '=' << x+y << endl << len(x) + len(y) + len(x+y) + 2;
    else if(op == 'b') cout << x << '-' << y << '=' << x-y << endl << len(x) + len(y) + len(x-y) + 2;
    else if(op == 'c') cout << x << '*' << y << '=' << x*y << endl <<len(x) + len(y) + len(x*y) + 2;
    return;
}
int main()
{
    cin >> n;
    while(n--)
    {
        readin();
        if(n != 0) cout << endl;
    }
    return 0;
}

不知道是哪里没有调整好,输出格式似乎不对。运行时间也长的不合理QWQ


by wsyhb @ 2020-12-24 10:14:41

@Slience 建议不要用 cin.putback() 这种东西,应使用字符串读入,然后将其转化成整数。


by 童年的小士兵 @ 2020-12-26 16:53:48

@Slience 负号也算在长度之内,abs去掉


|