CCF-CSP题解 201903-2 二十四点
2024-08-31 05:08:41
可枚举。
写栈的主要思想是:一个数栈\(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;
}
最新文章
- Encountered an unexpected error when attempting to resolve tag helper directive &#39;@addTagHelper&#39; with value &#39;";*, Microsoft.AspNet.Mvc.TagHelpers";&#39;
- 我的微信小程序入门踩坑之旅
- node.js自动化测试断言包assert的方法说明
- Python编码问题整理
- Oracle日期格式转换
- Leetcode 416. Partition Equal Subset Sum
- 转: Oracle Form 中commit 与do_key(&#39;commit_form&#39;)区别
- 【转】SQL Server T-SQL写文本文件
- ubuntu12.04之后该死的文件关联
- codevs 1036 商务旅行 (倍增LCA)
- 简单天气应用开发——自定义TableView
- Opencv实现图像的灰度处理,二值化,阀值选择
- C语言库函数大全及应用实例十
- appserv+win8
- HTML基础加强
- ubuntu 14.04安装mysql数据库
- Spring Cloud Config 配置中心高可用
- c#系统消息类封装
- WCF 学习笔记一
- PAT 1033. To Fill or Not to Fill (贪心)
热门文章
- MySQL 5.7 - 通过 BINLOG 恢复数据
- 更新centos7的kernel
- 微博验证码的识别并登录获取cookies
- JS的DOM操作语法
- Anaconda中启动Python时的错误:UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0xaf in position 553
- Java基础IO类之字节输入输出流
- 修改Linux克隆的物理地址 和 IP地址
- 面试官:JVM锁优化都优化了啥?
- zsh: /usr/local/bin/pod: bad interpreter: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: no such file or directory
- mongoDB学习笔记(一)之操作符