728x90
반응형
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
using namespace std;
// [백준 10773 c++ O] 제로
// 문제: 숫자를 입력받아 저장하되 0이 입력되면 저장된 수중 가장 나중의 수 제거 후 숫자들의 합 구하기
// 접근: 스택
// 풀이:
// 스택에 숫자들 입력
// 반복문 이용해 숫자들의 합 구하기
//주의: 스택을 탐색할 때는 while 쓰는것 좋은듯 , for 쓰면 st의 사이즈 저장해놓고 사용
int k;
stack<long long> st;
int main()
{
ios::sync_with_stdio(false); // 계산시간 단축 // 문제마다 오류 유무 다름
cin.tie(NULL); cout.tie(NULL);// 입출력 시간 단축
cin >> k;
for (int i = 0; i < k; i++) {
int num;
cin >> num;
if (num == 0) { st.pop(); continue; }
st.push(num);
}
// 스택에 있는 수의 합
long long sum = 0;
//cout << st.size() << '\n';
while (!st.empty()) {
sum += st.top();
st.pop();
}
cout << sum;
return 0;
}
|
cs |
반응형
'Algorithm_BOJ(백준) > 구현(자료구조)(Data structure)' 카테고리의 다른 글
[백준 10866 c++ O] 덱 (0) | 2021.07.27 |
---|---|
[백준 10845 c++ O] 큐 (0) | 2021.07.27 |
[백준 9093 c++ V] 단어 뒤집기 (0) | 2021.07.22 |
[백준 2504 c++ *] 괄호의 값 (0) | 2021.05.15 |
[백준 1620 c++] 나는야 포켓몬 마스터 이다솜 (0) | 2021.01.31 |