CCF-CSP题解 201903-2 二十四点
可枚举。
写栈的主要思想是:一个数栈\(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;
}
CCF-CSP题解 201903-2 二十四点的更多相关文章
- 第十六次 ccf 201903-2 二十四点
题意: 计算数学表达式的值, 数学表达式的定义: 4个[0-9]表示数字的字符 ,3个[+-x/]表示运算的字符 可以用正则为: ([0-9][+-x/]){3}[0-9] 例如: 5+2/1x3 2 ...
- 201903-2 CCF 二十四点
题面: 考场写的30分== #include<bits/stdc++.h> using namespace std; stack<int>st; stack<char&g ...
- [LeetCode] 24 Game 二十四点游戏
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- 二十四点算法 java实现
问题: 给出四个数,不可以重复使用,可以用+ - * /和括号,怎么得出24? 代码: //return -1 表示当前方法不行 private int workByStep(int op,int n ...
- HNU 12886 Cracking the Safe 二十四点的判断
经典的一个题,今天竟然写跪了…… 题意: 给你4个数字,让你判断是否能通过四则运算和括号,凑成24点. 思路: 暴力枚举运算顺序和运算符. 代码: #include <iostream> ...
- CSP201903-2二十四点
如图所示先处理乘号和除号,再处理加减. #include<bits/stdc++.h> using namespace std; ];int main(){ int n; cin>& ...
- 201903-2 二十四点 Java
思路: 数据结构中,栈可以解决运算的问题.利用压栈和弹栈操作实现(这里用队列模拟).具体的: 遇到乘除号,弹出栈顶元素,将计算结果压入栈中.遇到加减号,将后面的数一起压入栈中. 注意: substri ...
- CCF201903-2二十四点
思路描述:最开始的思路是拿一个栈来存储数据和符号,在动手实践的过程中发现行不通,单个数字的char和int转换可以,但是加起来的数据两位数字就很难处理了. 然后就去看了看别人的思路,给了我一个很好的启 ...
- CCF CSP 201503-3 节日
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201503-3 节日 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形 ...
随机推荐
- Linux菜鸟——常见命令一 查看及创建目录文件等命令
命令行提示符 [root@localhost ~]# [当前用户名@主机名 当前所在目录]$ linux 超级用户 root window 超级用户 administartor # 超级用户 $ 普通 ...
- 剑指Offer-25.复杂链表的复制(C++/Java)
题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否则 ...
- 菜鸟系列Fabric源码学习—orderer服务启动
Fabric 1.4 orderer 服务启动流程 1.提要 orderer提供broadcast和deliver两个服务接口.orderer节点与各个peer节点通过grpc连接,orderer将所 ...
- SpringBoot之DispatcherServlet详解及源码解析
在使用SpringBoot之后,我们表面上已经无法直接看到DispatcherServlet的使用了.本篇文章,带大家从最初DispatcherServlet的使用开始到SpringBoot源码中Di ...
- /etc/security/limits.conf配置文件详解
这个文件主要是用来限制用户对系统资源的使用.是/lib64/security/pam_limits.so模块对应的/etc/serurity/pam_limits的配置文件. # /etc/secur ...
- Electron存储简单数据和用户首选项推荐用electron-store
electron-store1可以用来保存Electron应用程序或模块的简单数据持久性-保存和加载用户首选项,应用程序状态,缓存等. 1https://github.com/sindresorhus ...
- Android ListView的header footer设置visibility gone不起作用
常用的ViewGroup,例如LinearLayout,在onMeasure方法内对每个child view执行measure前,会判断child view的visibility是否为gone.如果是 ...
- sina中的附件图片处理
这样写就会频繁的创建和销毁对象 因为setPhotos这个方法调用频繁 如果在里面直接用for循环创建9个UIImageView如果因为cell重用 比如在上一个cell中本来就有UIImageVie ...
- 小白的springboot之路(三)、集成mybatis与MySQL
0.前言 mybatis属于半自动的ORM,相比hibernate这种全自动的ORM,兼顾了性能与易用:目前企业项目中,基本都是mybatis的天下:今天就来整合mybatis与MySQL: 1.整合 ...
- Netty学习——Google Protobuf使用方式分析和环境搭建
Google Protobuf使用方式分析 在RPC框架中,Google Protobuf是很常用的一个库,和Apache Thrift 是同款的用于进行序列化的第三方库.原理都是大同小异,无非就是使 ...