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语言与智能技术竞赛的三元组抽取比赛方面的一次尝试.由于该比赛早已结束,笔者当时也没有参加这个比赛,因此没有测评成绩,我们也只能拿到训练集和验证集.但是,这并不耽误我们在这 ...
随机推荐
- beyond compare 4.2.9桌面右键集成的问题修复
beyond compare 4.2.9桌面右键集成的问题修复 安装后,发现在WIN64时,注册表中注册的DLL库有问题 出错处: [HKEY_CLASSES_ROOT\CLSID\{57FA2D12 ...
- 【EWM系列】SAP EWM创建warehouse task的函数
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP EWM创建warehouse ...
- 20190928 On Java8 第二十三章 注解
第二十三章 注解 定义在 java.lang 包中的5种标准注解: @Override:表示当前的方法定义将覆盖基类的方法.如果你不小心拼写错误,或者方法签名被错误拼写的时候,编译器就会发出错误提示. ...
- Struts2异常:HTTP Status 404 - There is no Action mapped for action name addBook.
HTTP Status 404 - There is no Action mapped for action name addBook. 在地址栏进行访问的时候,出现了这个错误信息,导致出现此异常的原 ...
- 来自python自学者的小问题
我想使用python的第三方库,但是我的IDE给我一个错误代码: D:\untitled\venv\Scripts\python.exe "D:/py code/venv/sxsxsxsxs ...
- Redis介绍及入门安装及使用
Redis介绍及入门安装及使用 什么是Redis Redis is an open source (BSD licensed), in-memory data structure store, use ...
- SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一
SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 pom引用 <?xml version=& ...
- hdu-2389.rain on your parade(二分匹配HK算法)
Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Ot ...
- Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer
原文:Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...
- C# String的几种比较方法对比(Compare,CompareTo, CompareOrdinal、Equals)
原文:http://blog.csdn.net/wushang923/article/details/7527499 注意点:切换方法的时候要注意返回值引起的变化!!! 1.Compare会通过传递进 ...