Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Solution {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- for (int a = 1; a <= 10; a++) {
- int n = sc.nextInt();
- String s = sc.next();
- String news = "";
- Stack<Character> stack = new Stack<Character>();
- for (int b = 0; b < n; b++) {
- if (s.charAt(b) >= '0' && s.charAt(b) <= '9') {
- news += s.charAt(b);
- } else if (s.charAt(b) == '(') {
- stack.add(s.charAt(b));
- } else if (s.charAt(b) == ')') {
- while (!(stack.peek() == '(')) {
- news += stack.pop();
- }
- stack.pop();
- } else if (s.charAt(b) == '+') {
- while (!(stack.peek() == '(')) {
- news += stack.pop();
- }
- stack.add(s.charAt(b));
- } else {
- stack.add(s.charAt(b));
- }
- }
- while (!stack.isEmpty()) {
- news += stack.pop();
- }
- Stack<String> stringstack = new Stack<String>();
- for (int b = 0; b < news.length(); b++) {
- if (news.charAt(b) >= '0' && news.charAt(b) <= '9') {
- stringstack.add(news.substring(b, b + 1));
- } else if (news.charAt(b) == '+') {
- int z = Integer.parseInt(stringstack.pop());
- int y = Integer.parseInt(stringstack.pop());
- int x = z + y;
- stringstack.add(String.valueOf(x));
- } else {
- int z = Integer.parseInt(stringstack.pop());
- int y = Integer.parseInt(stringstack.pop());
- int x = z * y;
- stringstack.add(String.valueOf(x));
- }
- }
- int sum = Integer.parseInt(stringstack.pop());
- System.out.println("#" + a + " " + sum);
- }
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment