为啥呀呜呜呜就两个没过了

P1957 口算练习题

a2021150044 @ 2022-07-28 23:11:49

#include<iostream>
using namespace std;
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<stack>
#include<stdio.h>
int length(int n) {
    int num = 0;
    if (n < 0) {
        n = -n;
        num++;
    }
    while (n) {
        num++;
        n /= 10;
    }
    return num;
}
int main() {
    int t;
    cin >> t;
    char c;
    int a, b;
    string s;
    while (t--) {
        cin >> s;
        if (s == "a" || s == "b" || s == "c") {
            c = s[0];
            cin >> a >> b;
        }
        else {
            int flag = 1;
            if (s[0] == '-') { 
            a = 0;  
            flag = -1;
            }
            else {
                a = s[0] - '0';
            }
            for (int i = 1; i < s.size(); i++) {
                a = a * 10 + s[i] - '0';
            }
            a *= flag;
            cin >> b;
        }
        int len = length(a) + length(b)+2;
        if (b < 0) len -= 1;
        if (c == 'a') {
            cout << a << "+" << b << "=" << a+b<<endl;
            len += length(a + b);
            cout << len << endl;
        }
        else if (c == 'b') {
            cout << a << "-" << b << "=" << a - b << endl;
            len += length(a - b);
            cout << len << endl;
        }
        else {
            cout << a << "*" << b << "=" << a * b << endl;
            len += length(a * b);
            cout << len << endl;
        }
    }
}

by HearTheWindSing @ 2022-07-28 23:25:54

@a2021150044 题目中说运算数是非负整数


by Kketchup @ 2022-07-29 07:26:20

@a2021150044

特判 $n=0$ 时 $num=1

by SegTree @ 2022-07-29 07:43:22

n=0n.\text{length}=1,但你的代码会输出 0

应该为:


int length(int n) {
    int num = 0;
    if (n == 0) {
        return 1;
    }
    while (n) {
        num++;
        n /= 10;
    }
    return num;
}

by SegTree @ 2022-07-29 07:43:35

@a2021150044


by a2021150044 @ 2022-07-29 16:08:15

@jpb_Saturn 谢谢啦,已经改好了,不过你给我的这段应该是你n<=0时num=1。


by a2021150044 @ 2022-07-29 16:11:05

//改了一下,去掉了多余的头文件
//没看到收入的是非负数qwq
#include<iostream>
using namespace std;
int length(int n) {
    int num = 0;
    if (n <= 0) num = 1;
    while (n) {
        num++;
        n /= 10;
    }
    return num;
}
int main() {
    int t;
    cin >> t;
    char c;
    int a, b;
    string s;
    while (t--) {
        cin >> s;
        if (s == "a" || s == "b" || s == "c") {
            c = s[0];
            cin >> a >> b;
        }
        else {
            a = s[0] - '0';
            for (int i = 1; i < s.size(); i++) {
                a = a * 10 + s[i] - '0';
            }
            cin >> b;
        }
        int len = length(a) + length(b)+2;
        if (c == 'a') {
            cout << a << "+" << b << "=" << a+b<<endl;
            len += length(a + b);
            cout << len << endl;
        }
        else if (c == 'b') {
            cout << a << "-" << b << "=" << a - b << endl;
            len += length(a - b);
            cout << len << endl;
        }
        else {
            cout << a << "*" << b << "=" << a * b << endl;
            len += length(a * b);
            cout << len << endl;
        }
    }
}

|