2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈
秋实大哥搞算数
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/problem/show/1074
Description
Input
接下来每一行有一个长度不超过10^6的表达式,只包含数字和四则运算符号('+', '-', '*', '/')。
1000000000.
Output
对于每一个表达式,输出相应的结果,占一行。
保证运算及结果在long long范围以内。
Sample Input
12+5/4-1
4*5/3
Sample Output
6
HINT
题意
题解:
首先用一个栈把中序遍历变为后序遍历,然后再随便用栈搞一搞就好了
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1002001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/*
inline ll read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int buf[10];
inline void write(int i) {
int p = 0;if(i == 0) p++;
else while(i) {buf[p++] = i % 10;i /= 10;}
for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
printf("\n");
}
*/
//************************************************************************************** ll num[maxn];
//stack<ll> ss;
map<char,ll> op;
struct sta
{
int kiss=;
ll ss[maxn];
void pop()
{
//return ss[kiss];
kiss--;
}
void push(ll x)
{
ss[++kiss]=x;
}
ll top()
{
return ss[kiss];
}
int empty()
{
if(kiss==)
return ;
return ;
}
};
sta sss;
int check(ll k, char op2)
{
if(k!=op['+']&&k!=op['-']&&k!=op['*']&&k!=op['/'])
{
return ;
}
char op1;
int flag=;
if(k==op['/'])
{
op1='/';
flag=;
}
if(k==op['*'])
{
op1='*';
flag=;
}
if(k==op['-'])
{
op1='-';
flag=;
}
if(k==op['+'])
{
op1='+';
flag=;
}
if(!flag)
return ;
if (op1=='+'|| op1 == '-')
if (op2 == '*' || op2 == '/')
return -;
else
return ; if (op1 == '*' || op1 == '/')
if (op2 == '+' || op2 == '-')
return ;
else
return ;
}
int main()
{
op['+']=;
op['-']=;
op['*']=;
op['/']=;
int t;
cin>>t;
char s[];
while(t--)
{
while(!sss.empty())
sss.pop();
scanf("%s",s);
ll pre=;
int tot=;
int len=strlen(s);
for(int i=;i<len;i++)
{
if(s[i]!='+'&&s[i]!='-'&&s[i]!='*'&&s[i]!='/')
{
pre=pre*+s[i]-'';
}
else
{
sss.push(pre);
pre=;
while(!sss.empty()&&check(sss.top(),s[i])>=)
{
num[tot++]=sss.top();
sss.pop();
}
sss.push(op[s[i]]);
}
}
if(pre!=)
sss.push(pre);
while(!sss.empty())
{
num[tot++]=sss.top();
sss.pop();
} for(int i=;i<tot;i++)
{
if(num[i]!=op['+']&&num[i]!=op['-']&&num[i]!=op['*']&&num[i]!=op['/'])
{
sss.push(num[i]);
}
else
{
ll b=sss.top();
sss.pop();
ll a=sss.top();
sss.pop();
if(num[i]==op['+'])
sss.push(a+b);
else if(num[i]==op['-'])
sss.push(a-b);
else if(num[i]==op['*'])
sss.push(a*b);
else
sss.push(a/b);
}
}
printf("%lld\n",sss.top());
sss.kiss=;
}
}
2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈的更多相关文章
- 2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和
B - 秋实大哥与花 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...
- 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集
秋实大哥打游戏 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈
秋实大哥去打工 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交
E - 秋实大哥与家 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 SET的妙用
D - 秋实大哥与战争 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 变化版本的线段树,合并区间,单点查询
D - 秋实大哥与战争 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...
- 2015 UESTC 数据结构专题C题 秋实大哥与快餐店 字典树
C - 秋实大哥与快餐店 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 ...
- 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化
秋实大哥与小朋友 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...
- 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp
秋实大哥の恋爱物语 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...
随机推荐
- BeanPostProcessor的五大接口
BeanPostProcessor 关于对象初始化前后的回调. public interface BeanPostProcessor { //该方法在bean实例化完毕(且已经注入完毕),在after ...
- 牛B的日本精神
在汤森路透评选出的<2015全球创新企业百强>榜单里,日本以40家高居榜首,力压美国的35家.而中国内地无一入围. 在中国媒体上,我们见到的日本是“失去的20年”,经济衰退.创新能力丧 ...
- SSL handshake failed: SSL error: Key usage violation in certificate has been detected.
sudo apt-get install libneon27-dev cd /usr/libsudo mv libneon-gnutls.so.27 libneon-gnutls.so.27.olds ...
- FPGA与CPLD的概念及其区别
一.FPGA与CPLD的基本概念 1.CPLD CPLD主要是由可编程逻辑宏单元(LMC,Logic Macro Cell)围绕中心的可编程互连矩阵单元组成,其中LMC逻辑结构较复杂,并具有复杂的I/ ...
- 解读Linux命令格式(转)
解读Linux命令格式 环境 Linux HA5-139JK 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x8 ...
- selenium grid应用2-多节点执行用例
启动远程 node我们目前启动的 Hub 与 node 都是在一台主机.那么要在其它主机启动 node 必须满足以下几个要求: 1)本地 hub 主机与远程 node 主机之间可以相互 ping 通 ...
- C/C++——[03] 注释
C/C++源程序中被注释的内容不能被编译,被认为是不属于程序的一部分. C/C++的注释有两种写法: 多行注释:以 “ /*”开头,以“ */”结尾: #include <stdio.h> ...
- django入门--django-blog-zinnia搭建个人博客
1.安装python 选择合适python2.7及以上版本安装https://www.python.org/downloads/ 2.建立虚拟环境 这不是必须的,但是建议使用,为每个项目单独引入依赖, ...
- LGPL 与GPL的区别
GPL(GNU General Public License) 我们很熟悉的Linux就是采用了GPL.GPL协议和BSD, Apache Licence等鼓励代码重用的许可很不一样.GPL的出发点是 ...
- Jmeter组件和属性(二)
Jmeter脚本开发原则 简单.正确.高效.简单:去除无关的组件,同时能复用的尽量复用.正确:对脚本或者业务正确性进行必要的判断,不能少也不能多.(200),业务错误的情况下,也可能返回200,必须用 ...