201903-2 CCF 二十四点
题面:

考场写的30分==
#include<bits/stdc++.h>
using namespace std;
stack<int>st;
stack<char>op;
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
string s;
while(t--)
{
cin>>s; //cout<<s.length();
for(int i=; i<; i++)
{
//cout<<i<<endl;
if(s[i]>=''&&s[i]<='')
st.push(s[i]-'');
else if(s[i]=='/')
{
int x=st.top();
//cout<<x<<'\n';
int y=s[i+]-'';
st.pop();
st.push(x/y);
i++;
}
else if(s[i]=='x')
{
int x=st.top();
int y=s[i+]-'';
st.pop();
st.push(x*y);
//cout<<x<<y<<endl;
i++;
}
else
{
op.push(s[i]);
} }
while(!op.empty())
{
char c=op.top();
op.pop();
int x=st.top();
st.pop();
int y=st.top();
st.pop();
if(c=='+')
{
st.push(x+y);
}
else
{
st.push(y-x);
}
}
//cout<<st.top()<<'\n';
if(st.top()==)puts("Yes");
else puts("No");
st.pop();
}
} }
hack数据:3x8-3+3
处理加减法没按照从前向后规则,然后炸了QAQ
修改后满分代码:
#include<bits/stdc++.h>
using namespace std;
stack<int>st;
stack<char>op;
//stack<int>t;
int A[];
char B[];
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
string s;
while(t--)
{
cin>>s; // cout<<s<<'\n';
//cout<<s.length();
for(int i=; i<; i++)
{
//cout<<i<<endl;
if(s[i]>=''&&s[i]<=''){ st.push(s[i]-'');
}
else if(s[i]=='/')
{
int x=st.top();
//cout<<x<<'\n';
int y=s[i+]-'';
st.pop();
st.push(x/y);
i++;
}
else if(s[i]=='x')
{
int x=st.top();
int y=s[i+]-'';
st.pop();
st.push(x*y);
//cout<<x<<y<<endl;
i++;
}
else
{
op.push(s[i]);
} } int a=,b=;
while(!st.empty())
{
A[a++]=st.top();
st.pop();
}
while(!op.empty())
{
B[b++]=op.top();
op.pop();
}
//cout<<a<<' '<<b<<'\n';
a--;
b--;
int x,y;
while(b>=)
{
x=A[a];
y=A[a-];
if(B[b]=='+')
{
A[a-]=x+y;
}
else
{
A[a-]=x-y;
}
a--;
b--;
}
//cout<<st.top()<<'\n';
if(A[]==)puts("Yes");
else puts("No");
//st.pop();
}
} }
201903-2 CCF 二十四点的更多相关文章
- 第十六次 ccf 201903-2 二十四点
题意: 计算数学表达式的值, 数学表达式的定义: 4个[0-9]表示数字的字符 ,3个[+-x/]表示运算的字符 可以用正则为: ([0-9][+-x/]){3}[0-9] 例如: 5+2/1x3 2 ...
- [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> ...
- CCF-CSP题解 201903-2 二十四点
可枚举. 写栈的主要思想是:一个数栈\(numSta\),一个运算符栈\(opSta\).遇到一个运算符,就把之前优先级\(equal\ or\ greater\ than\)它的运算符处理掉. #i ...
- CSP201903-2二十四点
如图所示先处理乘号和除号,再处理加减. #include<bits/stdc++.h> using namespace std; ];int main(){ int n; cin>& ...
- 201903-2 二十四点 Java
思路: 数据结构中,栈可以解决运算的问题.利用压栈和弹栈操作实现(这里用队列模拟).具体的: 遇到乘除号,弹出栈顶元素,将计算结果压入栈中.遇到加减号,将后面的数一起压入栈中. 注意: substri ...
- CCF201903-2二十四点
思路描述:最开始的思路是拿一个栈来存储数据和符号,在动手实践的过程中发现行不通,单个数字的char和int转换可以,但是加起来的数据两位数字就很难处理了. 然后就去看了看别人的思路,给了我一个很好的启 ...
- NLP(二十六)限定领域的三元组抽取的一次尝试
本文将会介绍笔者在2019语言与智能技术竞赛的三元组抽取比赛方面的一次尝试.由于该比赛早已结束,笔者当时也没有参加这个比赛,因此没有测评成绩,我们也只能拿到训练集和验证集.但是,这并不耽误我们在这 ...
随机推荐
- 字符串 映射相应的 函数 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke
http://blog.csdn.net/qustdong/article/details/7267258 字符串驱动技术—— MethodAddress , MethodName , ObjectI ...
- Spring MVC @RequestBody自动转JSON HTTP415错误解决方法
转自:http://blog.csdn.net/tiantiandjava/article/details/46125141 项目中想用@RequestBody直接接收json串转成对象 网上查了使用 ...
- P5444 [APIO2019]奇怪装置
传送门 考虑求出最小的循环节 $G$ 使得 $t,t+G$ 得到的数对是一样的 由 $y \equiv t \mod B$ ,得到 $G$ 一定是 $B$ 的倍数,设 $zB=G$,则 $t,t+zB ...
- C# DataTable与实体的相互转换
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespac ...
- 12、前端知识点--MVVM模式
1.MVVM与MVC的区别是什么? 在MVC里,View是可以直接访问Model的!从而,View里会包含Model信息,不可避免的还要包括一些业务逻辑. MVC模型关注的是Model的不变,所以,在 ...
- java 企业 网站源码 后台 springmvc SSM 前台 静态化 代码生成器
前台: 支持四套模版, 可以在后台切换 系统介绍: 1.网站后台采用主流的 SSM 框架 jsp JSTL,网站后台采用freemaker静态化模版引擎生成html 2.因为是生成的html,所以访问 ...
- websocket无法注入bean问题解决方案
websocket服务端往往需要和服务层打交道,因此需要将服务层的一些bean注入到websocket实现类中使用,但是呢,websocket实现类虽然顶部加上了@Component注解,依然无法通过 ...
- 基本SQL查询语句
使用Emp表和Dept表完成下列练习 Emp员工表 empno ename job Mgr Hiredate Sal Comm Deptno 员工号 员工姓名 工作 上级编号 受雇日期 薪金 佣金 部 ...
- mssql 取数据指定条数(例:100-200条的数据)
方法1:临时表 * into #aa from table order by time-- 将top m笔插入 临时表 select * from #aa order by time desc --d ...
- linux下挂载U盘方法
1.使用 cat /proc/partitions 查看系统现在有哪些分区:[root@localhost ~]# cat /proc/partitions major minor #blocks ...