80分,求助dalao

P1957 口算练习题

fish_golden @ 2023-07-11 22:50:40

#include <bits/stdc++.h>
using namespace std;
int n;
int a, b;
string op, pre_op;
int main()
{
    scanf("%d", &n);
    while (n--)
    {
        cin >> op;
        string ans;
        if (op == "a")
        {
            cin >> a >> b;
            ans = to_string(a) + "+" + to_string(b) + "=" + to_string(a + b);
        }
        else if (op == "b")
        {
            cin >> a >> b;
            ans = to_string(a) + "-" + to_string(b) + "=" + to_string(a - b);
        }
        else if (op == "c")
        {
            cin >> a >> b;
            ans = to_string(a) + "*" + to_string(b) + "=" + to_string(a * b);
        }
        else
        {
            a = atoi(op.c_str());
            cin >> b;
            if (pre_op == "a")
                ans = to_string(a) + "+" + to_string(b) + "=" + to_string(a + b);
            else if (pre_op == "b")
                ans = to_string(a) + "-" + to_string(b) + "=" + to_string(a - b);
            else
                ans = to_string(a) + "*" + to_string(b) + "=" + to_string(a * b);
        }
        cout << ans << endl
             << ans.size() << endl;
        pre_op = op;
    }
    return 0;
}

dalao们帮我看看为什么有2个点WA了。


by small_stone @ 2023-07-12 10:41:34

如果有一行只有两个数字,那么op就是数字组成的,不是a,b,c中的任意一个。把op赋值给pre_op时特判掉这种情况即可


by small_stone @ 2023-07-12 10:45:16

贴下我改完的代码:

#include <bits/stdc++.h>
using namespace std;
int n;
int a, b;
string op, pre_op;
int main()
{
    scanf("%d", &n);
    while (n--)
    {
        bool flag = true;
        cin >> op;
        string ans;
        if (op == "a")
        {
            cin >> a >> b;
            ans = to_string(a) + "+" + to_string(b) + "=" + to_string(a + b);
        }
        else if (op == "b")
        {
            cin >> a >> b;
            ans = to_string(a) + "-" + to_string(b) + "=" + to_string(a - b);
        }
        else if (op == "c")
        {
            cin >> a >> b;
            ans = to_string(a) + "*" + to_string(b) + "=" + to_string(a * b);
        }
        else
        {
            flag = false;
            a = atoi(op.c_str());
            cin >> b;
            if (pre_op == "a")
                ans = to_string(a) + "+" + to_string(b) + "=" + to_string(a + b);
            else if (pre_op == "b")
                ans = to_string(a) + "-" + to_string(b) + "=" + to_string(a - b);
            else
                ans = to_string(a) + "*" + to_string(b) + "=" + to_string(a * b);
        }
        cout << ans << endl
             << ans.size() << endl;
        if (flag) pre_op = op;
    }
    return 0;
}

by small_stone @ 2023-07-12 10:51:22

@fish_golden

问天第一还是有实力的~(^_^)~

要不去打一下团队里的暑期比赛


|