CCF认证-201903-2 二十四点提示运行错误。。。求大佬解决

写bug战神 写bug战神 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include<iostream>
#include<stack>
using namespace std;
bool cmp(char top, char out)
{
if ((top == '+' || top == '-') && (out == '+' || out == '-'))
return true;
else if ((top == 'x' || top == '/') && (out == 'x' || out == '/'))
return true;
else if ((top == 'x' || top == '/') && (out == '+' || out == '-'))
return true;
else
return false;
}
int calculate(int nx, int nh, char sign)
{
switch (sign)
{
case '+': return nx + nh;break;
case '-': return nx - nh;break;
case 'x': return nx * nh;break;
case '/': return nx / nh;break;
}
}
char s[8];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
stack<int> num;
stack<char> sym;
while (!num.empty())
num.pop();
while (!sym.empty())
sym.pop();
for (int j = 0; j < 8; j++)
{
scanf("%c", &s[j]);
if (s[j] != '\n')
{
if (s[j] >= '0' && s[j] <= '9')
num.push(s[j] - '0');
else if (sym.empty())
{
sym.push(s[j]);
}
else
{
if (cmp(sym.top(), s[j]))
{
while (cmp(sym.top(), s[j]))
{
int nh = num.top();
num.pop();
int nx = num.top();
num.pop();
num.push(calculate(nx, nh, sym.top()));
sym.pop();
}
sym.push(s[j]);
}
else
sym.push(s[j]);
}
}
}
while (!sym.empty())
{
int nh = num.top();
num.pop();
int nx = num.top();
num.pop();
num.push(calculate(nx, nh, sym.top()));
sym.pop();
}
if (num.top() == 24)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}

...全文
评论 7
收藏 已收藏
分享
返回顶部
写bug战神
C++ 语言 65,214 成员 250,514 内容