首页 > 试题广场 >

不喜欢括号

[编程题]不喜欢括号
  • 热度指数:822 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
NowCoder从小就喜欢数学,喜欢在笔记里记录很多表达式。它觉得现在的表达式写法很麻烦,为了提高运算符优先级,不得不添加很多括号,不小心漏了一个右括号的话差之毫厘谬之千里。
因此他改用前缀表达式,例如`(2 + 3) * 4`写成`* + 2 3 4`,这样就能避免使用括号了。这样的表达式书写简单,但计算却不够直观。请你写一个程序帮他计算这些前缀表达式吧。

输入描述:
输入包含多组数据,每组数据包含两行。

第一行为正整数n(3≤n≤50),紧接着第二行包含n个由数值和运算符组成的列表。

“+-*/”分别为加减乘除四则运算,其中除法为整除,即“5/3=1”。


输出描述:
对应每一组数据,输出它们的运算结果。
示例1

输入

3
+ 2 3
5
* + 2 2 3
5
* 2 + 2 3

输出

5
12
10

python解法,使用递归来做的:

从左到右读,如果遇到加减乘除符号,向后看两个,如果这两个数都是整数,那么直接开始计算。计算完后,进行下一个递归。

def calc(arr):
    for i,v in enumerate(arr):
        if v in ["+","-","*","/"]:
            if arr[i+1].isnumeric() and arr[i+2].isnumeric():
                return calc(arr[:i]+[str(int(eval(arr[i+1]+v+arr[i+2])))]+arr[i+3:])
    return arr[0]


while True:
    try:
        a,b=input(),input().split()
        print(calc(b))
    except:
        break
发表于 2017-10-13 08:55:08 回复(1)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			String[] arr = new String[n];
			Stack<Integer> stack = new Stack<>();
			for (int i = 0; i < n; i ++ )
				arr[i] = sc.next();
			for (int i = n - 1; i >= 0; i -- ) {
				if(arr[i].equals("+")) stack.push(stack.pop() + stack.pop());
				else if(arr[i].equals("-")) stack.push(stack.pop() - stack.pop());
				else if(arr[i].equals("*")) stack.push(stack.pop() * stack.pop());
				else if(arr[i].equals("/")) stack.push(stack.pop() / stack.pop());
				else stack.push(Integer.parseInt(arr[i]));
			}
			System.out.println(stack.pop());
		}
	}
}

发表于 2016-10-16 18:58:52 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <iomanip>
#include <memory>
#include <sstream>
#include <list>

using namespace std;

int main(int argc, char** argv)
{
	//freopen("in.txt", "r", stdin);
	int n;
	while (cin >> n)
	{
		vector<string> pre;
		string s;
		for (int i = 0; i < n; ++i)
		{
			cin >> s;
			pre.emplace_back(s);
		}
		while (pre.size() > 1)
		{
			for (int i = pre.size() - 1; i >= 0;--i)
			{
				string op = pre[i];
				if (op == "+" || op == "-" || op == "*" || op == "/")
				{
					int x = stoi(pre[i + 1]), y = stoi(pre[i + 2]);
					switch (op[0])
					{
					case '+': x += y; break;
					case '-': x -= y; break;
					case '*': x *= y; break;
					case '/': x /= y; break;
					default:break;
					}
					pre[i] = to_string(x);
					pre.erase(pre.begin() + i + 1, pre.begin() + i + 3);
					break;
				}
			}
		}

		cout << pre[0] << endl;
	}

	return 0;
}

发表于 2017-07-11 10:57:40 回复(0)
L0L头像 L0L
//利用堆栈;从右向左扫面输入的字符串:如果是数字的话,数字进栈;
//符号的话,弹出栈顶的两个元素做运算,运算结果进栈
#include<iostream>
#include<stack>
#include<string>
#include<vector>
using namespace std;
int main() {
	int n,i,a,b,t;
	
	stack<int> ret;

	while(cin>>n) {
		if(n==0)
			continue;
		vector<string> v(n);
		i=0;
		while(i<n) {
			cin>>v[i++];
		}
		for(i=n-1; i>=0; i--) {
			if(v[i].compare("+")==0) {
				a=ret.top();
				ret.pop();
				b=ret.top();
				ret.pop();
				ret.push(a+b);
			} else if(v[i].compare("-")==0) {
				a=ret.top();
				ret.pop();
				b=ret.top();
				ret.pop();
				ret.push(a-b);
			} else if(v[i].compare("*")==0) {
				a=ret.top();
				ret.pop();
				b=ret.top();
				ret.pop();
				ret.push(a*b);
			} else if(v[i].compare("/")==0) {
				a=ret.top();
				ret.pop();
				b=ret.top();
				ret.pop();
				ret.push(a/b);
			} else {
			//	cout<<v[i]<<endl;
				ret.push(atoi(v[i].c_str()));
			}
		}
		cout<<ret.top()<<endl;
		v.clear();
		while(!ret.empty()){
			ret.pop();
		}
	}
	return 0;
}

发表于 2015-09-09 14:37:45 回复(1)
import sys

while True:
import sys


while True:
    try:
        # 从右向左扫描字符串,遇到操作符比较操作符合栈顶元素的优先级,
        n = int(input())
        s = input()
        L = s.split()
        L = L[::-1]
        operations = ["+", "-", "*", "/"]
        stack1 = []
        stack2 = []

        for i in L:
            if i in operations:
                x = stack2.pop()
                y = stack2.pop()
                z = eval(x + i + y)
                stack2.append(str(z))
            else:
                stack2.append(i)
        print(stack2.pop())
        #print(str(int(stack2.pop())))
    except:
        break

   
编辑于 2023-03-16 15:38:43 回复(1)
// write your code here cpp
#include<cstdio>
(802)#include<stack>
#include<cstring>
(803)#include<cctype>
using namespace std;
int main(){
    char str[60][20];
    int n;
    while(scanf("%d",&n)!=EOF){
        stack<int> s;
        for(int i=0;i<n;++i){
            scanf("%s",str[i]);
        }
        for(int i=n-1;i>=0;--i){
            if(isdigit(str[i][0])){
                int sum=0;
                for(int j=0;j<strlen(str[i]);++j){
                    sum=sum*10+str[i][j]-'0';
                }
                s.push(sum);
            }else{
                int a=s.top();
                s.pop();
                int b=s.top();
                s.pop();
                int sum;
                if(str[i][0]=='+')
                    sum=a+b;
                else if(str[i][0]=='-')
                    sum=a-b;
                else if(str[i][0]=='*')
                    sum=a*b;
                else
                    sum=a/b;
                s.push(sum);
            }
        }
        printf("%d\n",s.top());
    }
}

发表于 2020-03-20 00:20:27 回复(0)
#include <iostream>
#include<stdlib.h>
using namespace std;
int main()
{
 int n;
    int a[51] = { 0 };
 int b[51] = { 0};
 while (cin >> n&&n>=3&&n<=50) {
  int i;
  int k;
  int j;
  int h;
  char c[50][20];
  for (i = 0; i < n; i++) {
   cin >>c[i];
   if (c[i][0] == '+') {
    a[i] = '+';
   }
   else if (c[i][0] == '-') {
    a[i] = '-';
   }
   else if (c[i][0] == '*') {
    a[i] = '*';
   }
   else if (c[i][0] == '/') {
    a[i] = '/';
   }
   else {
    a[i] = atof(&c[i][0]);
   }
   
  }
  for (j = i - 1,k=0; j >= 0; j--) {
 
   if (char(a[j])== '+'||char(a[j]) == '-'|| char(a[j] )== '*'||char(a[j])== '/') {
    if (char(a[j]) == '+')
     b[k - 2] = b[k - 1] + b[k - 2];
    else if (char(a[j]) == '-')
     b[k - 2] = b[k - 1] - b[k - 2];
    else if (char(a[j]) == '*')
     b[k - 2] = b[k - 1] * b[k - 2];
    else if (char(a[j]) == '/') {
     if (b[k - 2] == 0)
      continue;
     else
      b[k - 2] = b[k - 1] / b[k - 2];
    }
    k = k - 1;
   }
   else {
    b[k] = a[j];
    k++;
    
   }
  }
  cout << b[0] << endl;
 }
}


发表于 2019-05-19 16:32:44 回复(0)
import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            Stack<Node> nodeStack = new Stack<>();
            Node root = null;
            for (int i = 0; i < n; i++) {
                if (i == 0) {
                    Node newNode = new Node(sc.next());
                    nodeStack.push(newNode);
                    root = newNode;
                } else {
                    Node pNode = nodeStack.peek();
                    Node newNode = new Node(sc.next());
                    if (pNode.lchild == null) {
                        pNode.lchild = newNode;
                    } else if (pNode.rchild == null) {
                        pNode.rchild = newNode;
                        nodeStack.pop();
                    }
                    if (!newNode.isLeaf) {
                        nodeStack.push(newNode);
                    }
                }
            }
            System.out.println(root.getValue());
        }
        sc.close();
    }

    static class Node {
        Node lchild, rchild;
        String val;
        int intVal;
        boolean isLeaf = true;
        public Node(String val) {
            this.val = val;
            try {
                this.intVal = Integer.parseInt(this.val);
            } catch (Exception e) {
                isLeaf = false;
            }
        } 
        int getValue() {
            if (isLeaf) {
                return intVal;
            }
            switch (val) {
                case "+":
                    return lchild.getValue() + rchild.getValue();
                case "-":
                    return lchild.getValue() - rchild.getValue();
                case "*":
                    return lchild.getValue() * rchild.getValue();
                default:
                    return lchild.getValue() / rchild.getValue();
            }
        }
    }
}
构建二叉树,叶子节点是数字,非叶子节点是符号,这样子输入实际上是一个扩展二叉树序列,最后从根节点开始计算表达式的值
编辑于 2018-08-23 13:43:10 回复(0)
#include <bits/stdc++.h>
using namespace std;

long long atoi(string s) {
    long long res = 0;
    for (int i = 0; i < s.size(); ++i) {
        res = res * 10 + s[i] - '0';
    }
    return res;
}

int main() {
    int n;
    while (cin >> n) {
        string s[50];
        stack<long long> num;
        for (int i = 0; i < n; ++i) {
            cin >> s[i]; 
        }
        for (int i = n - 1; i >= 0; --i)
            if (s[i] == "+" || s[i] == "-" || s[i] == "*" || s[i] == "/") {
                long long left = num.top();
                num.pop();
                long long right = num.top();
                num.pop();
                if (s[i] == "+")
                    left += right;
                else if (s[i] == "-") 
                    left -= right;
                else if (s[i] == "*") 
                    left *= right;
                else 
                    left /= right;
                num.push(left);
            }
            else {
                num.push(atoi(s[i]));
            }
        cout << num.top() << endl;
    }
    return 0;
}

发表于 2018-02-25 07:44:44 回复(0)
#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;

int com(stack<char>n, int num){
int res;
stack<char>tem;
while (!n.empty()){
if ((n.top() - '0' >= 0) && (n.top() - '0' <= 9))
{
char b = n.top();
tem.push(b);
n.pop();
}
else if (n.top() == '+'){
int add1 = tem.top()-'0';
tem.pop();
int add2 = tem.top() - '0';
tem.pop();
char add3 = add1 + add2+'0';
tem.push(add3);
n.pop();
}
else if (n.top() == '-'){
int sub1 = tem.top() - '0';
tem.pop();
int sub2 = tem.top() - '0';
tem.pop();
char sub3 = sub1 - sub2+'0';
tem.push(sub3);
n.pop();
}
else if (n.top() == '*'){
int mul1 = tem.top() - '0';
tem.pop();
int mul2 = tem.top() - '0';
tem.pop();
char mul3 = mul1 * mul2+'0';
tem.push(mul3);
n.pop();
}
else if (n.top() == '/'){
int div1 = tem.top() - '0';
tem.pop();
int div2 = tem.top() - '0';
tem.pop();
if (div2 != 0){
char div3 = div1 / div2 + '0';
tem.push(div3);
n.pop();
}
}

}
res = tem.top()-'0';
return res;
}


int main(){
int num;
while (cin >> num){
stack<char>res;
char a;
for (int i = 0; i < num; i++)
{
cin >> a;
res.push(a);
}
cout << com(res,num) << endl;
}
return 0;
}
发表于 2017-04-17 14:29:07 回复(0)
import java.util.Scanner;
import java.util.Stack;

public class Main {
	private static Scanner scan = new Scanner(System.in);
	public static void main(String[] args) {
		while(scan.hasNext()){
			Stack<String> stackStr = new Stack<>();//暂存所有字符
			Stack<Integer> stack = new Stack<>();//暂存运算数字
			
			int n = Integer.parseInt(scan.next());
			for(int i = 1; i <= n; ++i)
				stackStr.push(scan.next());
			
			for(int i = 1; i <= n; ++i){
				String str = stackStr.pop();
				//判断是否是数字
				if (Character.isDigit(str.charAt(0))) {
					stack.push(Integer.parseInt(str));
				} else {
					char c = str.charAt(0);
					int a = stack.pop();
					int b = stack.pop();
					switch (c) {
					case '+':
						stack.push(a+b);
						break;
					case '-':
						stack.push(a-b);
						break;
					case '*':
						stack.push(a*b);
						break;
					case '/':
						stack.push(a/b);
						break;
					}
				}
			}
			System.out.println(stack.pop());
		}
	}
}


发表于 2016-08-20 15:26:42 回复(0)
这个代码输入哪个“前缀表达式”会发生溢出?

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <functional> #include <algorithm> #include <numeric> #include <stack> #include <queue> #include <vector> #include <string> #include <sstream> using namespace std; void myPush (stack<char> &st, char bas); char cal (int one, int two, char opchar); char cal (int one, int two, char opchar) { switch (opchar) { case '+': return one + two + 48; case '-': return one - two + 48; case '*': return one * two + 48; case '/': { if (0 == two) { return 0; } return one / two + 48; } default: { cout << "输入有误" << endl; return 0; } } } void myPush (stack<char> &st, char bas) { if (st.empty () || st.top () < '0' || st.top () > '9' || bas < '0' || bas > '9') { st.push (bas); } else { int one = st.top () - 48; int two = bas - 48; st.pop (); char opchar = st.top (); st.pop (); char ret = cal (one, two, opchar); myPush (st, ret); } } int main () { int n; while (scanf ("%d", &n) != EOF) { char bas; stack<char> st; for (int x = 0; x < n; x++) { cin >> bas; myPush (st, bas); } cout << st.top() - 48 << endl; } return 0; }

发表于 2016-07-27 23:53:20 回复(1)
import java.util.Scanner;
import java.util.Stack;
public class Main {
	public static void main(String[] args) {
		try(Scanner scanner = new Scanner(System.in)){
			while(scanner.hasNext()){
				int n = Integer.parseInt(scanner.next());
				Stack<String> inStack = new Stack<>();
				Stack<Integer> stack = new Stack<>();
				for(int i=0;i<n;i++){
					String input = scanner.next();
					inStack.push(input);
				}
				while(inStack.size()>0){
					String input = inStack.pop();
					char c = input.charAt(0);
					if(c>='0'&&c<='9'){
						stack.push(Integer.parseInt(input));
					}else{
						int a = stack.pop();
						int b = stack.pop();
						switch(c){
						case '+':stack.push(a+b);break;
						case '-':stack.push(a-b);break;
						case '*':stack.push(a*b);break;
						case '/':stack.push(a/b);break;
						}
					}
				}
				System.out.println(stack.pop());
			}
		}
	}
}

发表于 2015-09-18 15:55:41 回复(0)

问题信息

难度:
13条回答 6519浏览

热门推荐

通过挑战的用户

查看代码