File tree Expand file tree Collapse file tree 3 files changed +104
-0
lines changed
Concept/01_Queue_Stack/SampleCode/C++ Expand file tree Collapse file tree 3 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 1+ /* *
2+ * 문제: 제로 (http://boj.kr/10773)
3+ * Tier: Silver 4 (2021.01.14 기준)
4+ * Comment: 0이 나올때 가장 최근의 값을 뺍니다.
5+ * 즉, FIFO 구조를 띄고 있고, 자연스럽게 스택을 사용하시면 됩니다.
6+ */
7+
8+ #include < iostream>
9+ #include < stack>
10+ using namespace std ;
11+
12+ int N;
13+ stack<int > st;
14+
15+ int main () {
16+ ios_base::sync_with_stdio (false );
17+ cin.tie (0 );
18+
19+ cin >> N;
20+
21+ for (int i = 0 ; i < N; i++) {
22+ int tmp; cin >> tmp;
23+ if (tmp == 0 ) st.pop ();
24+ else st.push (tmp);
25+ }
26+
27+ long long result = 0 ;
28+ while (!st.empty ()) {
29+ result += st.top ();
30+ st.pop ();
31+ }
32+
33+ cout << result;
34+ }
Original file line number Diff line number Diff line change 1+ /* *
2+ * 문제: 과제는 끝나지 않아! (http://boj.kr/17952)
3+ * Tier: Silver 3 (2021.01.14 기준)
4+ * Comment: 하다가 새로운 것이 들어오면 이어서 하는 것이 아니라
5+ * 새로 받은 것 부터 한다고 합니다.
6+ * 당연히 스택이겠죠?
7+ */
8+
9+ #include < iostream>
10+ #include < stack>
11+ using namespace std ;
12+
13+ int score = 0 ;
14+ int N;
15+ stack<pair<int , int >> st;
16+
17+ int main () {
18+ ios_base::sync_with_stdio (false );
19+ cin.tie (0 );
20+
21+ cin >> N;
22+
23+ for (int i = 0 ; i < N; i++) {
24+ int x, point, minute;
25+ cin >> x;
26+
27+ if (x == 1 ) {
28+ cin >> point >> minute;
29+ st.push ({point, minute});
30+ }
31+
32+ if (!st.empty () && --st.top ().second == 0 ) {
33+ score += st.top ().first ;
34+ st.pop ();
35+ }
36+ }
37+
38+ cout << score;
39+ }
Original file line number Diff line number Diff line change 1+ /* *
2+ * 문제: 괄호 (http://boj.kr/9012)
3+ * Tier: Silver 4 (2021.01.14 기준)
4+ * Comment: 스택 하면 떠올릴 수 있는 가장 유명한 문제입니다.
5+ * 그런데, 어차피 괄호 종류는 하나인데 굳이 스택을 써야 할까요?
6+ */
7+
8+ #include < iostream>
9+ using namespace std ;
10+
11+ int main () {
12+ ios_base::sync_with_stdio (false );
13+ cin.tie (0 );
14+
15+ int N; cin >> N;
16+
17+ for (int i = 0 ; i < N; i++) {
18+ string str; cin >> str;
19+ int cnt = 0 ;
20+ for (char c: str) {
21+ if (c == ' (' ) cnt++;
22+ else if (c == ' )' && cnt) cnt--;
23+ else {
24+ cnt = -999 ;
25+ break ;
26+ }
27+ }
28+ if (cnt == 0 ) cout << " YES\n " ;
29+ else cout << " NO\n " ;
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments