c语言30分求助TT测试点3数据本地试过输出完全一致但是WA了是为什么TT

P1957 口算练习题

bekkii25 @ 2024-10-25 00:29:10

#include<stdio.h>
#include<string.h>
int main() {
    int n;
    scanf("%d", &n);
    char a[50][25] = { 0 };
    char y;//y储存上一轮符号字符 
    int b[50] = { 0 };
    int c[50] = { 0 };
    int i;
    for (i = 0; i < n; i++) {
        scanf("%s", a[i]);
        if (a[i][0] != 'a' && a[i][0] != 'b' && a[i][0] != 'c') {
            int j = strlen(a[i]);
            int k = 0;
            b[i] = 0;
            for (k; k < j; k++) {
                b[i] = b[i] * 10 + a[i][k]-'0';
            }
            a[i][0] = y;
            scanf("%d", &c[i]);
        }
        else {
            y = a[i][0];
            scanf("%d%d", &b[i], &c[i]);
        }
    }
    for (i = 0; i < n; i++) {
        int z;
        if (a[i][0] == 'a') {
            z = printf("%d+%d=%d\n", b[i], c[i], b[i] + c[i]);
        }
        else if (a[i][0] == 'b') {
            z = printf("%d-%d=%d\n", b[i], c[i], b[i] - c[i]);
        }
        else if (a[i][0] == 'c') {
            z = printf("%d*%d=%d\n", b[i], b[i], b[i] * c[i]);
        }
        printf("%d\n", z - 1);
    }
    return 0;
}

by 1357911BCC @ 2024-10-25 20:11:56

#include <bits/stdc++.h>
using namespace std;
int n;
string s;
char ch;

void _ys(char op,string a,string b){
    if(op == 'a'){
        string c = to_string(stoi(a) + stoi(b));
        int lena = a.size(),lenb = b.size(),lenc = c.size();
        cout << a << '+' << b << '=' << c << endl << lena + lenb + lenc + 2 << endl;
    }
    else if(op == 'b'){
        string c = to_string(stoi(a) - stoi(b));
        int lena = a.size(),lenb = b.size(),lenc = c.size();
        cout << a << '-' << b << '=' << c << endl << lena + lenb + lenc  + 2 << endl;
    }
    else {

        string c = to_string(stoi(a) * stoi(b));
        int lena = a.size(),lenb = b.size(),lenc = c.size();
        cout << a << '*' << b << '=' << c << endl << lena + lenb + lenc  + 2 << endl;
    }
}

int main() {
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> s;
        if(s[0] == 'a' || s[0] == 'b' || s[0] == 'c') {
            ch = s[0];
            string a,b;
            cin >> a >> b;
            _ys(s[0],a,b);
        }
        else {
            string b;
            cin >> b;
            _ys(ch,s,b);
        }
    }
    return 0;
}

用string更方便


|