D - Stack

Write a program which reads an expression in the Reverse Polish notation and prints the computational result.

An expression in the Reverse Polish notation is calculated using a stack. To evaluate the expression, the program should read symbols in order. If the symbol is an operand, the corresponding value should be pushed into the stack. On the other hand, if the symbols is an operator, the program should pop two elements from the stack, perform the corresponding operations, then push the result in to the stack. The program should repeat this operations.

Input

An expression is given in a line. Two consequtive symbols (operand or operator) are separated by a space character.

You can assume that +, - and * are given as the operator and an operand is a positive integer less than 106

Output

Print the computational result in a line.

Constraints

2 ≤ the number of operands in the expression ≤ 100

1 ≤ the number of operators in the expression ≤ 99

-1 × 109 ≤ values in the stack ≤ 109

Sample Input 1

1 2 +

Sample Output 1

3

Sample Input 2

1 2 + 3 4 - *

Sample Output 2

-3


解题心得:

  1. 给出的输入很简单已经是一个逆波兰表示法了,只要写一个栈来模拟一下运算的过程。
  2. 如果输入的是四则运算符号,就从栈中取出两个数字运算,得出的结果压入栈,如果输入的直接就是数字,就直接压入栈。

#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
stack <long long> st;
while(cin>>s)
{
if(s[0]>='0' && s[0] <= '9')
{
long long num = 0;
for(int i=0;i<s.length();i++)
{
long long temp = (s[i]-'0');
num = num*10+temp;
}
st.push(num);
}
else
{
long long a,b;
b = st.top();
st.pop();
a = st.top();
st.pop();
if(s[0] == '*')
a = a*b;
else if(s[0] == '+')
a = a+b;
else if(s[0] == '-')
a = a-b;
else if(s[0] == '/')
a = a/b;
st.push(a);
}
}
long long ans = st.top();
st.pop();
printf("%lld\n",ans);
return 0;
}

Aizu-ALDS1_3_A:Stack的更多相关文章

  1. Aizu - 2564 Tree Reconstruction 并查集

    Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...

  2. Aizu - 2555 Everlasting Zero 模拟

    Aizu - 2555 Everlasting Zero 题意:学习技能,每个技能有不同的要求,问能否学习全部特殊技能 思路:枚举每两个技能,得到他们的先后学习关系,如果两个都不能先学的话就是No了, ...

  3. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  4. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  5. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  6. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

  7. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  8. Stack操作,栈的操作。

    栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...

  9. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  10. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

随机推荐

  1. bzoj2740 串 && bzoj2176 strange string(最小表示法模板)

    https://konnyakuxzy.github.io/BZPRO/JudgeOnline/2740.html 题解讲的很清楚了 (好像等于的情况应该归入case2而不是case1?并不确定) 具 ...

  2. JavaScript alert()函数

    avaScript alert()函数 alert--弹出消息对话框(对话框中有一个OK按钮) alert,中文"提醒"的意思 alert函数语法 alert(str); aler ...

  3. DB2中横表纵表互换

    1.列转行:创建一个如下的表drop table dwtmp.tmp_xn_lsb; create table dwtmp.tmp_xn_lsb ( year      int ,quarter   ...

  4. android开发学习 ------- 上传本地项目到gitlab

    写了一个demo,上传到gitlab 参考  https://blog.csdn.net/litianxiang_kaola/article/details/74075151 1:新建项目,填写相应的 ...

  5. kafka基础二

    kafka生产者工作流程 消息产生分析 1.写入方式: producer采用推(push)模式将消息发布到broker,每条消息都会被追加(append)到分区Partition上,属于顺序写磁盘(顺 ...

  6. Android Studio你必须学会的快捷键(Eclipse转AS必看)

    前言:从Eclipse转到Android Studio之后,一开始把keymap设置成Eclipse,却发现有些常用的快捷键都失效了,大概是冲突了.想了下,觉得与其重新设置快捷键,不如去适应AS的快捷 ...

  7. windows Git的安装和使用

    一.本人是根据廖雪峰大神的个人官网学习的git,并有感而发以做笔记的方式写下这篇博客,希望可以帮助到自己和其他人,廖雪峰个人官网http://www.liaoxuefeng.com/ 二.声明我的电脑 ...

  8. 【数据库-Azure SQL Database】JDBC 如何连接 SQL Azure 数据库

    使用 JAVA 代码连接 Azure SQL Database 时产生了 SSL 错误,对于此问题大多数用户都是因为不知如何编写 JDBC 连接字符串而产生的,以下为相关示例代码,供您参考:   pa ...

  9. 使用canvas能画各种各样的东西

    用过canvas的人都知道,在这个画布上面可以制作各种各样的动画效果,想必大家都用过这个. 晒晒刚刚用这个做的一个demo: 现在来画一个圆看看: demo.js: var can,ctx,count ...

  10. Linux OpenGL 实践篇-14-多实例渲染

    多实例渲染 OpenGL的多实例渲染是一种连续执行多条相同的渲染命令的方法,并且每条命令产生的结果都有轻微的差异,通常用于渲染大量的几何物体. 设想一个场景,比如太空,我们需要渲染数以万记的星球,如果 ...