[백준][Java] 1935번 후위 표기식2 (스택)

[백준][Java] 1935번 후위 표기식2 (스택)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Stack; public class Main { private static BufferedReader br = new BufferedReader( new InputStreamReader( System . in )); private static BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( System . out )); public static void main( String [] args) throws IOException{ int N = stoi(br.readLine()); char [] str = br.readLine().toCharArray(); double [] num = new double [ 26 ]; for ( int i = 0 ; i < N; i + + ) num[i] = Double.parseDouble(br.readLine()); Stack < Double > st = new Stack < > (); for ( char c : str) { if ( 'A' < = c & & c < = 'Z' ) { st.push(num[c - 'A' ]); } else { if (c = = '+' ) { st.push(st.pop() + st.pop()); } else if (c = = '-' ) { double back = st.pop(); double front = st.pop(); st.push(front - back); } else if (c = = '*' ) { st.push(st.pop() * st.pop()); } else if (c = = '/' ) { double back = st.pop(); double front = st.pop(); st.push(front / back); } } } System . out . println ( String . format ( "%.2f" , st.pop())); // bw.write(""); // bw.flush(); // bw.close(); } private static int stoi( String input) { return Integer. parseInt (input); } } Colored by Color Scripter

from http://aig2029.tistory.com/337 by ccl(A) rewrite - 2021-10-13 20:01:41