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 ...
随机推荐
- Linux内核中实现生产者与消费者(避免无效唤醒)【转】
转自:http://blog.csdn.net/crazycoder8848/article/details/42581399 本文关注的重点是,避免内核线程的无效唤醒,并且主要是关注消费者线程的设计 ...
- 有关mysql的innodb_flush_log_at_trx_commit参数【转】
一.参数解释 0:log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行.该模式下在事务提交的时候,不会主动触发写入磁盘的操作. 1:每次事务 ...
- 在shell中如何判断字符串是否为有效的IP地址【转】
转自 在shell中如何判断字符串是否为有效的IP地址_echoisecho_新浪博客http://blog.sina.com.cn/s/blog_53a844e50100xxus.html 近来需要 ...
- Class.this 和 this 的有什么不同
转载:http://www.cnblogs.com/liwei45212/archive/2013/04/17/3026364.html 在阅读Java代码的時候 我们时会看到Class.this的使 ...
- Java的BIO,NIO,AIO
Java中的IO操作可谓常见.在Java的IO体系中,常有些名词容易让人困惑不解.为此,先通俗地介绍下这些名词. 1 什么是同步? 2 什么是异步? 3 什么是阻塞? 4 什么是非阻塞? 5 什么是同 ...
- Oracle创建WM_CONCAT函数
Oracle创建WM_CONCAT函数 WM_CONCAT这个函数会出错,所以从 11g开始.官方不认可 WM_CONCAT.然后就没这个函数了, 下面就是创建WM_CONCAT这个函数的步骤 第一步 ...
- 将table导出为excel格式文件
html: <table cellpadding="0" cellspacing="0" class="data_table" id= ...
- 使用 redis 减少 秒杀库存 超卖思路
由于数据库查询的及插入的操作 耗费的实际时间要耗费比redis 要多, 导致 多人查询时库存有,但是实际插入数据库时却超卖 redis 会有效的减少相关的延时,对于并发量相对较少的 可以一用 publ ...
- csu 1547(01背包)
1547: Rectangle Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 996 Solved: 277[Submit][Status][Web ...
- MIAC HW2
MIAC的第二次作业,翻了一些fashion网站找了点灵感,重新设计了一下UI. 因为给的html里有nav之类的HTML5新特性,所以索性就不管IE的兼容了.chrome下的效果: FF下也差不多. ...