错误求调

P1957 口算练习题

ChaojideXiao_Yu @ 2024-09-01 11:41:20

#include<bits/stdc++.h>
using namespace std;
int n, flag;
int cnt1, cnt2;
string str;
int size(int num) {
    int ans = 0;
    if(num <= 0) {
        ans = 1;
        num = abs(num);
    }
    else ans = 0;
    while(num != 0)
        num /= 10, ans += 1;
    return ans;
}
int main() {
    cin >> n;
    getchar();
    for(int i = 1; i <= n; i++) {
        getline(cin, str);
        if(str[0] == 'a') {
            flag = 1, cnt1 = cnt2 = 0;
            bool sign = false;
            for(int j = 2; j <= str.size() - 1; j++) {
                if(str[j] == ' ') {
                    sign = true;
                    continue;
                }
                else if(sign == true) cnt2 = cnt2 * 10 + str[j] - '0';
                else cnt1 = cnt1 * 10 + str[j] - '0';
            }
            cout << cnt1 << '+' << cnt2 << '=' << cnt1 + cnt2 << endl;
            cout << str.size() - 1 + size(cnt1 + cnt2) << endl;
        }
        else if(str[0] == 'b') {
            flag = 2, cnt1 = cnt2 = 0;
            bool sign = false;
            for(int j = 2; j <= str.size() - 1; j++) {
                if(str[j] == ' ') {
                    sign = true;
                    continue;
                }
                else if(sign == true) cnt2 = cnt2 * 10 + str[j] - '0';
                else cnt1 = cnt1 * 10 + str[j] - '0';
            }
            cout << cnt1 << '-' << cnt2 << '=' << cnt1 - cnt2 << endl;
            cout << str.size() - 1 + size(cnt1 - cnt2) << endl;
        }
        else if(str[0] == 'c') {
            flag = 2, cnt1 = cnt2 = 0;
            bool sign = false;
            for(int j = 2; j <= str.size() - 1; j++) {
                if(str[j] == ' ') {
                    sign = true;
                    continue;
                }
                else if(sign == true) cnt2 = cnt2 * 10 + str[j] - '0';
                else cnt1 = cnt1 * 10 + str[j] - '0';
            }
            cout << cnt1 << '*' << cnt2 << '=' << cnt1 * cnt2 << endl;
            cout << str.size() - 1 + size(cnt1 * cnt2) << endl;
        }
        else {
            if(flag == 1) {
                flag = 1, cnt1 = cnt2 = 0;
                bool sign = false;
                for(int j = 2; j <= str.size() - 1; j++) {
                    if(str[j] == ' ') {
                        sign = true;
                        continue;
                    }
                    else if(sign == true) cnt2 = cnt2 * 10 + str[j] - '0';
                    else cnt1 = cnt1 * 10 + str[j] - '0';
                }
                cout << cnt1 << '+' << cnt2 << '=' << cnt1 + cnt2 << endl;
                cout << str.size() - 1 + size(cnt1 + cnt2) << endl;
            }
            else if(flag == 2) {
                flag = 2, cnt1 = cnt2 = 0;
                bool sign = false;
                for(int j = 2; j <= str.size() - 1; j++) {
                    if(str[j] == ' ') {
                        sign = true;
                        continue;
                    }
                    else if(sign == true) cnt2 = cnt2 * 10 + str[j] - '0';
                    else cnt1 = cnt1 * 10 + str[j] - '0';
                }
                cout << cnt1 << '-' << cnt2 << '=' << cnt1 - cnt2 << endl;
                cout << str.size() - 1 + size(cnt1 - cnt2) << endl;
            }
            else if(flag == 3) {
                flag = 1, cnt1 = cnt2 = 0;
                bool sign = false;
                for(int j = 2; j <= str.size() - 1; j++) {
                    if(str[j] == ' ') {
                        sign = true;
                        continue;
                    }
                    else if(sign == true) cnt2 = cnt2 * 10 + str[j] - '0';
                    else cnt1 = cnt1 * 10 + str[j] - '0';
                }
                cout << cnt1 << '*' << cnt2 << '=' << cnt1 * cnt2 << endl;
                cout << str.size() - 1 + size(cnt1 * cnt2) << endl;
            }
        }
    }
    return 0;
}

最后7、8、9、10个点错误


by ChaojideXiao_Yu @ 2024-09-01 11:51:32

得了60分


by zsq_br @ 2024-09-01 12:38:47

纯模拟 可用switch


by ChaojideXiao_Yu @ 2024-09-02 18:24:56

谢谢


|