40WA求助 感觉代码没问题啊

P1957 口算练习题

~~样例没过$=$没问题~~ 字符串转int写错了
by L_cm_C5H6 @ 2022-04-07 19:53:42


@[L_cm_C5H6](/user/429699) ```cpp #include <bits/stdc++.h> using namespace std; int n;//n道题 string str;//输入的字符串 char k;//接收一个运算符 int x, y;//两个数 int main() { cin >> n;//输入n道题 for (int i = 0; i < n; i++) { cin >> str;//输入一个字符串 (定义为字符串是因为有可能为数字) if (str[0] == 'a' || str[0] == 'b' || str[0] == 'c')//判断是不是运算符 { k = str[0]; cin >> x >> y; } else { x = 0; for (int i = 0; i < str.size(); i++) { x = x * 10 + (str[i] - '0'); } cin >> y; } int ans;//运算结果 if (k == 'a') { ans = x + y; cout << x << "+" << y << "=" << x + y << endl; } else if (k == 'b') { ans = x - y; cout << x << "-" << y << "=" << x - y << endl; } else if (k == 'c') { ans = x * y; cout << x << "*" << y << "=" << x * y << endl; } int cnt = 0;//记录数字个数 if (ans < 0) cnt++;//ans < 0 多一个负号 while(x != 0) { x = x / 10; ++cnt; } while(y != 0) { y = y / 10; ++cnt; } while(ans != 0) { ans = ans / 10; ++cnt; } cout << 2 + cnt << endl; } return 0; } ``` 你好,这个是改过转化int代码以后的, 但是只有80,我找了半天实在是找不到错误的地方了,是哪里还不严谨吗?
by jingchuan_Hou @ 2022-04-08 19:51:32


@[jingchuan_Hou](/user/711574) 运算数为0的没算到算式长度中
by L_cm_C5H6 @ 2022-04-09 00:43:02


@[L_cm_C5H6](/user/429699) 嗯嗯,已经解决了忘了回复你,真的很感谢你,谢谢你!
by jingchuan_Hou @ 2022-04-10 16:24:59


|