Aizu-ALDS1_3_A:Stack
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
解题心得:
- 给出的输入很简单已经是一个逆波兰表示法了,只要写一个栈来模拟一下运算的过程。
- 如果输入的是四则运算符号,就从栈中取出两个数字运算,得出的结果压入栈,如果输入的直接就是数字,就直接压入栈。
#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的更多相关文章
- Aizu - 2564 Tree Reconstruction 并查集
Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...
- Aizu - 2555 Everlasting Zero 模拟
Aizu - 2555 Everlasting Zero 题意:学习技能,每个技能有不同的要求,问能否学习全部特殊技能 思路:枚举每两个技能,得到他们的先后学习关系,如果两个都不能先学的话就是No了, ...
- 线性数据结构之栈——Stack
Linear data structures linear structures can be thought of as having two ends, whose items are order ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder
Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- Stack操作,栈的操作。
栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
随机推荐
- python tickle模块与json模块
#! /usr/bin/env python# -*- coding:utf-8 -*-#JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式 ...
- python入门之实例-商品选择
需求: 显示一系列商品,根据序号选择商品 li = ["手机","电脑","电视"] #函数enumerate在for循环遍历的时候,会默认 ...
- Oracle Database Hang While Loading 3rd party SBT Library And After This Nobody Can Access The Database (windows login 登陆hang )
Applies to: Oracle Database - Enterprise Edition - Version 11.2.0.4 and later Microsoft Windows x64 ...
- JAVA常用知识总结(七)——Spring
如果一个接口有2个不同的实现, 如何Autowire某一个指定的实现? 1.通过增加@Qualifier(实现类的名字): @Autowired @Qualifier("GirlStuden ...
- 字符串与C51的格式化输出
一字符数组和字符指针: 字符指针可以用字符串对其直接初始化和随时赋值:而字符数组可以用字符串进行初始化,但不能用字符串对其进行随时赋值(但此时可以定义一个字符串指针指向字符数组,然后用字符串对指针随时 ...
- AQS及其前置知识总结
CLH队列锁 及自旋锁 乐观锁及CAS 独占锁与共享锁 LockSupport与wait ,join和notify 这里截取内部类Node的部分代码,节点的状态值如下: /** waitStatus ...
- Codeforces 371BB. Fox Dividing Cheese
Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, corre ...
- DrawTool画笔之纹理笔
先上图: 今天我们要实现的是DrawTool画笔集合中的一种纹理笔,很多人可能对纹理笔概念还比较生疏,其实如果你接触过类似一些教育行业交互式白板的话,对纹理笔并不会感到陌生,纹理笔我们可以简单的理解为 ...
- Brackets前端开发编辑器
http://www.cnblogs.com/xiazdong/p/3550148.html http://blog.csdn.net/shinesun001/article/details/5348 ...
- Java编程基础-方法
1.方法(函数)概要 (1).含义:方法(函数)就是定义在类中的具有特定功能的一段独立小程序. (2).方法定义的语法格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参 ...