自家学校OJ网站上的题目,很玄学,不知道哪里出的问题。
这个OJ链接的题目没有问题的。
https://www.luogu.org/problemnew/show/P1981
#include <iostream> #include <string> #include <math.h> #include <stack> using namespace std; bool compare(char now, char stack) { switch (now) { case '+': if (stack == '#') return false; return true; case '*': if (stack == '+' || stack == '-' || stack == '#') return false; return true; break; } } int calc(int a, char c, int b) { switch (c) { case '+': return (a % 10000 + b % 10000) % 10000; case '*': return (a % 10000 * b % 10000) % 10000; default: break; } } int main() { int Int_temp, i = 0; string s; stack<int>num; stack<char>symbol; getline(cin, s); //s.append(1, '#'); symbol.push('#'); while (i < s.length()) { if (s[i] >= '0'&&s[i] <= '9') { string Str_temp; Int_temp = 0; while (s[i] >= '0'&&s[i] <= '9') { Int_temp = Int_temp * 10 + (s[i] - 48); i++; } num.push(Int_temp); Str_temp.clear(); } else { if (compare(s[i], symbol.top())) { int t1 = num.top(); num.pop(); int t2 = num.top(); num.pop(); num.push(calc(t1, symbol.top(), t2)); symbol.pop(); } //if (s[i] != '#') symbol.push(s[i]); i++; } } while (symbol.top() != '#') { int t1 = num.top(); num.pop(); int t2 = num.top(); num.pop(); num.push(calc(t1, symbol.top(), t2)); symbol.pop(); } cout << num.top() % 10000 << endl; return 0; }
题目有很简单的做法,我想复杂了,但思路也是没有错的。交了不过,因为从校园网上Dlowand了测试数据对自己特别自信 ,发现测试数据特别 特别长,用控制台一次输入上限是4095个字符,再往后键盘就按不动了,后面的数据理所当然就没输入进去,我也重新用JAVA写了一遍,也证明了我的算法是对的。
然后,我就果断 学校的垃圾OJ网站 坑骗无知儿童 ,上网找了一下:https://blog.csdn.net/u014542643/article/details/78435113 ,交了发现AC了。
我对比了一下,可能让我的交不过的差别就是,我在字符串最后append 一个 '#' ,仔细想了下,确实加了是多余的,我把'#'去掉,交了也还真就过了,我不知道是什么原理,我想可能和缓冲区有关,也许是玄学吧。