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 ...
随机推荐
- Bootstrap文件上传组件:bootstrap fileinput
为了上传预览pdf与图片特用此插件. 源码以及API地址: bootstrap-fileinput源码:https://github.com/kartik-v/bootstrap-fileinput ...
- 用C#实现对MSSqlServer数据库的增删改查---DAL层
说明:本人完成的工作是对传感器--超声波物位计进行硬件集成,上位机通过串口接收传感器数据并将其存到数据库中:在DAL层实现对数据库的增删改查,其中包含两个数据表分别是WaterLevelSet表和Wa ...
- Firefox缓存文件夹位置设置及清除缓存方法
地址栏敲入: about:config, 新建一个"browser.cache.disk.parent_directory", 并设置为你要的缓存文件夹, 例如: "F ...
- vsts 自动部署到Azure
如果要部署到中国区的Azure ,请先阅读 http://www.cnblogs.com/cnryb/p/7867275.html 前置条件,我把代码托管在vsts(放在GitHub上也没问题,这里不 ...
- java版云笔记(九)之动态sql
SQL 首先,所谓SQL的动态和静态,是指SQL语句在何时被编译和执行,二者都是用在SQL嵌入式编程中的,这里所说的嵌入式是指将SQL语句嵌入在高级语言中,而不是针对于单片机的那种嵌入式编程. 静态S ...
- centos6.5 使用 rpm 安装 mysql
从mysql网站下载mysql rpm安装包(包括server.client) 1.安装server rpm -ivh MySQL-server-5.6.19-1.el6.x86_64.rpm 强制安 ...
- java基础12 抽象类(及关键字:abstract)
抽象类:abstract 1.应用的场景 我们描述一类事物时,存在着某种行为,但这种行为目前不具体,那么我们就可以抽取这种行为的声明,但是不去实现这种行为,我们就需要使用抽象类. 2.抽象的好处 强制 ...
- scala学习5--函数二
to def test() : Unit = { // for(i <- 1.to(100)){ // println(i) // } for(i <- 1 to 100 ){ prin ...
- 号外,号外 -几乎所有的binary search和mergesort都有错
号外,号外 -几乎所有的binary search和mergesort都有错 这是Joshua Bloch(Effective Java的作者)在google blog上发的帖子.在说这个帖子之前,不 ...
- **CodeIgniter通过hook的方式实现简单的权限控制
根据自己的实际情况,需要两个文件,一个是权限控制类,Acl,另外一个是权限配置的文件acl.php放在了config这个目录下. Acl这个类放在了application/hook/acl.php.通 ...