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 ...
随机推荐
- callee与caller
1.callee arguments.callee表示当前函数,使用于递归 function factorial(num){ if(num<=1){ return 1; }else{ retur ...
- 统计oracle表中字段的个数
select count(column_name) from user_tab_columns where table_name='emp' dba权限对应的视图是dba_tab_columns 和a ...
- BurpSuite 设置Hostname Resolution
#写在前面 这种情况你可能遇到过: 对方用了CDN, 你查到了对方真实IP, 但还不能100%肯定. 这时候, 最好的测试就是 win/linux修改HOST文件 Win重启电脑 Linux重启网络 ...
- 9 - Python函数定义-位置参数-返回值
目录 1 函数介绍 1.1 为什么要使用函数 1.2 Python中的函数 2 函数的基本使用 3 函数的参数 3.1 参数的默认值 3.2 可变参数 3.2.1 可变位置传参 3.2.2 可变关键字 ...
- 去除\ufeff的解决方法,python语言
语言:python 编程工具:pycharm 硬件环境:win10 64位 读取文件过程中发现一个问题:已有记事本文件(非空),转码 UTF-8,复制到pycharm中,在开始位置打印结果会出现 \ ...
- 生成器(generator)和迭代(iterable , iterator, iteration)
在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...
- 报错:Cobbler check 时报错
报错:[root@test88 ~]# cobbler checkTraceback (most recent call last): File "/usr/bin/cobbler&quo ...
- PlantUML——3.Graphviz的安装
官网:http://www.graphviz.org/Home.php 由于plantuml使用Graphviz来生成相关图形(只有序列图可以不依赖它),其它图形都需要, 因此得安装它,否则生成图 ...
- sshd_config OpenSSH SSH 进程配置文件配置说明
名称 sshd_config – OpenSSH SSH 服务器守护进程配置文件 大纲 /etc/ssh/sshd_config 描述sshd 默认从 /etc/ssh/sshd_config 文件( ...
- PTP简介
PTP简介 在通信网络中,许多业务的正常运行都要求网络时钟同步,即整个网络各设备之间的时间或频率差保持在合理的误差水平内.网络时钟同步包括以下两个概念: l 时间同步:也叫相 ...