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 ...
随机推荐
- CSS 中 nth-child 和 nth-of-type 的区别
假设有如下代码结构,想要查找 Piggy 那个 p <section> <h1>Words</h1> <p>Little</p> <p ...
- 摘: 给Shapre命名
有两种解决方式: 在 VBA 中将slide中的Shape命名,改变shape.name即可. 另外一种方式就是有点投机取巧:你可以点击shap,右键选择web/alternativetext做些标记 ...
- Linux运维常见问题解决集锦【转】
作为linux运维,多多少少会碰见这样那样的问题或故障,用点心,平时多注意积累,水平肯定越来越高. 下面就是常见问题解决集锦: 1.shell脚本不执行 问题:某天研发某同事找我说帮他看看他写的s ...
- NGUI优化之Drawcall
今天在运行之前的程序时,无意中发现一个简单的menu菜单页面drawcall居然达到接近30了,这个数值感觉太高了. 后网上查询关于降低drawcall的方法,发现主要有以下几点: 1.少用Panel ...
- python【项目】:选课系统【简易版】
功能要求 角色:学校.学员.课程.讲师要求:1. 创建学校2. 创建课程3. 课程包含,周期,价格,通过学校创建课程4. 通过学校创建班级, 班级关联课程.讲师5. 创建学员时,选择学校,关联班级5. ...
- web 端 gantt组件选型
gantt - 甘特图 甘特图(Gantt chart)又称为横道图.条状图(Bar chart).其通过条状图来显示项目,进度,和其他时间相关的系统进展的内在关系随着时间进展的情况.以提出者亨利·L ...
- hdu 5895(矩阵快速幂+欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895 f(n)=f(n-2)+2*f(n-1) f(n)*f(n-1)=f(n-2)*f(n-1)+2 ...
- JAVA 抽象类、接口
一.抽象类 1.语法定义 在类前面使用 abstract 修饰的类就是抽象类 2.应用场景: (1)在某些情况下,某个父类只是知道其子类应该包含怎样的方法,但无法准确知道这些子类如何实现这些方法. ( ...
- Numpy narray对象的属性分析
参考官方文档链接: narray是Numpy的基本数据结构,本文主要分析对象的属性(可通过.进行访问) 1:导入numpy: import numpy as np 2:初始化narray对象: > ...
- python生成器 协程
生成器 参考文章: 协程 gevent 生成器进阶 看个例子: def gg(): n='' i=0 while True: n=yield i #通过send传入到n if not n: pa ...