반응형
public class Q_10828 {
	
	private int top;
	private int numOfData;
	private int[] stack;
	
	public Q_10828(int N) {
		top = -1;
		numOfData = 0;
		stack = new int[N];
	}
	
	public void push(int x) {
		top++;
		stack[top] = x;
		numOfData++;
	}
	
	public int isNull(int idx) {
		if(idx == -1 || stack[idx] == 0) {
			return -1;
		}
		return stack[idx];
	}
	
	public int pop() {
		if(top == -1) {
			return -1;
		}
		int rIdx = top;
		top--;	
		numOfData--;
		return isNull(rIdx);
	}
	
	public int size() {
		return numOfData;
	}
	
	public int empty() {
		if(numOfData == 0) {
			return 1;	
		}
		return 0;
	}
	
	public int top() {
		int peekIdx = top;
		return isNull(peekIdx);
	}
	
	public static void main(String[] args) throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int count = Integer.parseInt(br.readLine());
		Q_10828 q_10828 = new Q_10828(count);		
		String[] commandArr = new String[count];
		
		for (int i = 0; i < count; i++) {
			commandArr[i] = br.readLine();
		}
		
		for (int j = 0; j < commandArr.length; j++) {
			String[] commandSplit = commandArr[j].split(" ");
			switch (commandSplit[0]) {
			case "push": {
				q_10828.getClass().getMethod(commandSplit[0], int.class).invoke(q_10828, Integer.parseInt(commandSplit[1]));
				break;
			}
			default:
				System.out.println((int) q_10828.getClass().getMethod(commandSplit[0]).invoke(q_10828));
				break;
			}
		}
	}
}

 

URL :  https://www.acmicpc.net/problem/10828

반응형

+ Recent posts