可枚举。

写栈的主要思想是:一个数栈\(numSta\),一个运算符栈\(opSta\)。遇到一个运算符,就把之前优先级\(equal\ or\ greater\ than\)它的运算符处理掉。

#include <bits/stdc++.h>

using namespace std;

int operation(int num1, char op, int num2)
{
if (op == '+')
return num1 + num2;
else if (op == '-')
return num1 - num2;
else if (op == 'x')
return num1 * num2;
else
return num1 / num2;
} int getAns(char s[]) {
stack<int> numSta;
stack<char> opSta;
for (int i = 1; i <= 7; i++)
{
if (s[i] >= '1' && s[i] <= '9')
numSta.push(s[i] - '0');
else
{
if (s[i] == '+' || s[i] == '-')
{
while (!opSta.empty())
{
char op = opSta.top(); opSta.pop();
int num2 = numSta.top(); numSta.pop();
int num1 = numSta.top(); numSta.pop();
numSta.push(operation(num1, op, num2));
}
opSta.push(s[i]);
}
else
{
while (!opSta.empty() && (opSta.top() == 'x' || opSta.top() == '/'))
{
char op = opSta.top(); opSta.pop();
int num2 = numSta.top(); numSta.pop();
int num1 = numSta.top(); numSta.pop();
numSta.push(operation(num1, op, num2));
}
opSta.push(s[i]);
}
}
}
while (!opSta.empty())
{
char op = opSta.top(); opSta.pop();
int num2 = numSta.top(); numSta.pop();
int num1 = numSta.top(); numSta.pop();
numSta.push(operation(num1, op, num2));
}
return numSta.top();
} int main ()
{
int n;
scanf("%d", &n); while (n--)
{
char s[10];
scanf("%s", s + 1);
if (getAns(s) == 24)
printf("Yes\n");
else
printf("No\n");
} return 0;
}

最新文章

  1. Encountered an unexpected error when attempting to resolve tag helper directive &#39;@addTagHelper&#39; with value &#39;&quot;*, Microsoft.AspNet.Mvc.TagHelpers&quot;&#39;
  2. 我的微信小程序入门踩坑之旅
  3. node.js自动化测试断言包assert的方法说明
  4. Python编码问题整理
  5. Oracle日期格式转换
  6. Leetcode 416. Partition Equal Subset Sum
  7. 转: Oracle Form 中commit 与do_key(&#39;commit_form&#39;)区别
  8. 【转】SQL Server T-SQL写文本文件
  9. ubuntu12.04之后该死的文件关联
  10. codevs 1036 商务旅行 (倍增LCA)
  11. 简单天气应用开发——自定义TableView
  12. Opencv实现图像的灰度处理,二值化,阀值选择
  13. C语言库函数大全及应用实例十
  14. appserv+win8
  15. HTML基础加强
  16. ubuntu 14.04安装mysql数据库
  17. Spring Cloud Config 配置中心高可用
  18. c#系统消息类封装
  19. WCF 学习笔记一
  20. PAT 1033. To Fill or Not to Fill (贪心)

热门文章

  1. MySQL 5.7 - 通过 BINLOG 恢复数据
  2. 更新centos7的kernel
  3. 微博验证码的识别并登录获取cookies
  4. JS的DOM操作语法
  5. Anaconda中启动Python时的错误:UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0xaf in position 553
  6. Java基础IO类之字节输入输出流
  7. 修改Linux克隆的物理地址 和 IP地址
  8. 面试官:JVM锁优化都优化了啥?
  9. zsh: /usr/local/bin/pod: bad interpreter: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: no such file or directory
  10. mongoDB学习笔记(一)之操作符