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. Codeforces 1137B(kmp next数组构造)

    为了物尽其用,Next求出最多有哪部分能重复使用,然后重复使用就行了-- const int maxn = 5e5 + 5; char s[maxn], t[maxn]; int cnts0, cnt ...

  2. Codeforces 1139D(推式子+dp)

    题目传送 推公式博客传送 推完式子就是去朴素地求就行了Orz const int maxn = 1e5 + 5; const int mod = 1e9 + 7; int m, mu[maxn], v ...

  3. linux ln -s 软链接

     一.创建 ln -s 源文件 目标文件 当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目录下用ln ...

  4. 60分钟课程: 用egg.js实现增删改查,文件上传和restfulApi, webpack react es6 (一)

    今天开始我将写nodejs框架egg.js, react 实现的增删改查,文件上传等常用的b/s场景,这个将分3部分来写. 会让你在60分钟内快速 入口并应用~  你应该用es6, node,或是ph ...

  5. office 导出问题

    就用程序池右击项目高级设置 应用程序池的项目中的标识改为 LocalSystem 启用32位应用程序设为true或false

  6. eclipse修改xml配置文件tomcat不能同步问题

    之前springmvc-config.cml中的Controller写成了Constroller,导致java.lang.ClassNotFoundException异常,而我更改后,更新的信息并没有 ...

  7. LeetCode 100 及 101题

    100. 相同的树 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [ ...

  8. 本机运行x程序出现:Can't open display 原因及其解决方法(貌似非永久)

    http://blog.sina.com.cn/s/blog_53db572501016ma7.html 这是因为Xserver默认情况下不允许别的用户的图形程序的图形显示在当前屏幕上. 如果需要别的 ...

  9. Jenkins环境搭建(6)-修改自动化测试报告的样式

    写在最前: 我遇到一个问题,就是导出数据时,接口返回的数据是乱码,乱码如下图所示.问了开发,说是byte数据.这种情况,将response Data数据写入到报告中的话,在jenkins上运行时,提示 ...

  10. Android 面试总结~~~

    一.面试中的问题 通过这几天的面试,总结了自己在面试过程中问到的问题,部分问题已经给出了答案,还有部分问题,还未有时间整理出来. ListView出现闪图.图片错乱原因解决方案 函数式编程 (Lamb ...