P1957输出与样例一样判错?

P1957 口算练习题

ROMax @ 2023-04-16 09:54:13

#include <iostream>
#include <string>
#include <cctype>
#include <cstdio>

using namespace std;

char op[3] = {'+', '-', '*'};

int main(int argc, const char** argv)
{
    int n, i = 0, c;
    cin >> n;
    getchar();
    char str[51][15];
    for (int j = 0; j < n; j++){
        int c;
        while ((c = getchar()) != '\n') str[j][i++] = c;
        str[j][i] = '\n';
        i = 0;
    }
    int index = 0;
    for (int j = 0; j < n; j++){
        while ('\n' != str[j][i]) i++;
        int len = i;
        i = 0;
        int num1 = 0, num2 = 0;
        if (isalpha(str[j][0])){
            index = str[j][0] - 'a';
            i += 2;
        }
        int length = len - i + 1;
        while (' ' != str[j][i]) num1 = num1 * 10 + str[j][i++] - '0';
        i++;
        while ('\n' != str[j][i]) num2 = num2 * 10 + str[j][i++] - '0';

        int res;
        switch (index){
            case 0: res = num1 + num2;
                break;
            case 1: res = num1 - num2;
                break;
            default: res = num1 * num2;
                break;
        }

        if (res < 0) length++;

        std::printf("%d%c%d=%d\n", num1, op[index], num2, res);
        int count = 0;
        if (!res) count++;
        else{
            while (res){
                count++;
                res /= 10;
            }
        }
        std::printf("%d", count + length);
        if (n - 1 > j) putchar('\n');
        i = 0;
    }
    return 0;
}

|