zoukankan      html  css  js  c++  java
  • Stack Aizu

    Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free.

    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

    Notes

    Template in C

    code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00             ^   11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                ^ 00.                                     ^^0.^
                                 1 ^ 0                                     ^^110.^
                               0.   0 ^                                    ^^^10.01
                       ^^     010^   1 1                                   ^^^1110.1
                       0001  10 0   ^ 1.1                                   ^^^1111110
                       0^ 10 . 01   ^^  ^^                                   ^^^1111^1.^           ^^^
                       10  10^ 0^                                             ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // Virtual_Judge —— Stack Aizu - ALDS1_3_A.cpp created by VB_KoKing on 2019,04,28,22.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first hurricane that passed through your ear is you,
    The first star you see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    int top=0,S[1000];
    
    void push(int x)
    {
        //top+1之后将元素插入top所指的位置
        S[++top]=x;
    }
    
    int pop()
    {
        //返回top所指的元素
        top--;
        return S[top+1];
    }
    
    int main()
    {
        int a,b;
        char s[100];
    
        while (scanf("%s",s)!=EOF)
        {
            switch (s[0])
            {
                case '+':a=pop();b=pop();push(a+b);break;
                case '-':b=pop();a=pop();push(a-b);break;
                case '*':a=pop();b=pop();push(a*b);break;
                default:push(atoi(s));break;
            }
        }
        printf("%d
    ",pop());
        return 0;
    }
    
  • 相关阅读:
    解决在cmd命令下不能输入中文方法
    报错注入
    html表单中的name属性和value属性
    xss漏洞
    DVWA-xss反射型(跨站脚本漏洞)
    DVWA-brute force
    owsap top 10 2017(十大web安全应用程序安全)
    sqli_labs less-5
    盲注
    c++ 类
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338394.html
Copyright © 2011-2022 走看看