문제 : https://www.acmicpc.net/problem/10773

 

서론

 

스택문제.

STL을 사용하지 않고 간단한 스택을 만들어봤다.

다음문제는 연결리스트로 구현해볼 계획이다.

 

구현

 

//c++

#include <iostream>

using namespace std;

int stack[100001];
int top = -1;

void push(int input) {
	top++;
	stack[top] = input;
}

void pop() {
	top--;
}

int main() {
	int k;
	int result = 0;
	cin >> k;
	for (int i = 0; i < k; i++) {
		int tmp;
		cin >> tmp;
		if (tmp == 0) pop();
		else push(tmp);
	}
	for (int i = 0; i <= top; i++) result += stack[i];
	cout << result;
}