-
Notifications
You must be signed in to change notification settings - Fork 1
9-hadongun #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
9-hadongun #36
Conversation
dohyeondol1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์คํ ๊ธฐ๋ณธ ๋ฌธ์ ๋ ์ด์ ๋๋ฌด ๋ง์ด ํ์์ฃ
์ฌ ์ต์ํ๊ฒ ํ ์ ์๊ฒ ๋์์ผ๋ฆฌ๋ผ ์๊ฐํฉ๋๋ค.
ํ์ด ๋ฐฉ์์ ๋๋ถ๋ถ์ ์ฌ๋๋ค์ด ํฌ๊ฒ ๋ค๋ฅด์ง ์์ ๋ฐฉ์์ผ๋ก ํ์์ ๊ฒ ๊ฐ๋ค์.
์
๋ ฅ๋๋ ์ซ์๋ฅผ ์คํ์ push ํ๋ค๊ฐ, ์
๋ ฅ ๊ฐ์ด "0"์ผ ๊ฒฝ์ฐ pop ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์คํ์ ๋จ์ ์๋ค์ ํฉํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์์ต๋๋ค.
C++
#include <iostream>
#include <stack>
using namespace std;
int main() {
int K;
cin >> K;
stack<int> stack;
while(K--) {
int N;
cin >> N;
stack.push(N);
if(N == 0) {
stack.pop();
stack.pop();
}
}
int sum = 0;
while(!stack.empty()) {
sum += stack.top();
stack.pop();
}
cout << sum << '\n';
return 0;
}์ด์ ์คํ ๋ฌธ์ ๋ ๋์ด๋๋ฅผ ๋์ฌ๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค!
์์งํ ์ค๋ฒ 4 ~ 5 ์คํ ๋ฌธ์ ๋ง์ด ํ์๋ฐ ใ
ใ
?
๋ค์์๋ ๊ณจ๋ ๋ฌธ์ ๋ก ๋์ ~
์๊ฐ ์กฐ๊ธ๋ง ์ฐ๋ฉด ํ ์ ์์๊ฑฐ์์! ํฐ์ด๋ ๊ธ์์นํ๊ณ ๊ฐ๊ฟ~
์คํฐ์, ํ, ๊ดํธ์ ๊ฐ
|
์ ๋ ์ฒ์์ ๋ฌธ์ ๋ฅผ ๋ณด๊ณ , deque๋ฅผ ์ฌ์ฉํ ๊น ํ๋ค๊ฐ list์ ์๊ฐ๋ณต์ก๋๊ฐ ๋ณ ์ฐจ์ด๊ฐ ์์ ๊ฒ ๊ฐ์ ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ์ต๋๋ค. Python3
import sys
input=sys.stdin.readline
stack=[]
for _ in range(int(input())):
num = int(input())
if num==0:
stack.pop()
else:
stack.append(num)
print(sum(stack)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ต์ํ ๋ง, ๋๋จน๋ ๋ง์ด๋ผ์ ์ข๋ค์ ใ
ใ
ezํ๊ฒ ํ์ด๋ดค์ต๋๋ค.
C
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int N = 0;
scanf("%d", &N);
int* stack = (int*)malloc(sizeof(int) * N);
int input = 0;
int top = 0;
long long sum = 0;
for (int i = 0; i < N; i++)
{
scanf("%d", &input);
if (input == 0)
{
if (top == 0)
{
continue;
}
else
{
top--;
stack[top] = NULL;
}
}
else
{
stack[top++] = input;
}
}
for (int i = 0; i < top; i++)
{
sum = stack[i] + sum;
}
printf("%d", sum);
return 0;
}
Co-authored-by: Do Hyeonseok <[email protected]>
๐ ๋ฌธ์ ๋งํฌ
(https://www.acmicpc.net/problem/10773)
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
โจ ์๋ ์ฝ๋
์คํ ๋ฌธ์ ๊ฐ ํ๊ณ ์ถ์ด ๊ฐ์ ธ์์ต๋๋ค.
์ฒซ ์ค์ ์ ์ K๊ฐ ์ฃผ์ด์ง๊ณ , ์ดํ K์ค์ ์ ์๋ค์ด ์ฃผ์ด์ง๋ค. ์ด ์ ์๊ฐ 0์ด๋ฉด ๊ฐ์ฅ ์ต๊ทผ์ ์ด ์๋ฅผ ์ง์์ผ ํ๋ฏ๋ก
์ ๋ ฅ ๋ฐ์ ์๊ฐ 0์ด๋ฉด pop()์ ์คํํ๋ค. ์ด๋ฅผ K๋ฒ ๋ฐ๋ณตํ๋๋ก ๋ฐ๋ณต๋ฌธ์ ์ค์
K๋ฒ์ ๋ฐ๋ณต์ด ๋๋๋ฉด ์คํ ๋ด๋ถ์ ๋จ์์๋ ๊ฐ์ ๋ชจ๋ ๋ํด์ฃผ๊ธฐ ์ํด stack์ด ๋น ๋ ๊น์ง pop์ ํด์ฃผ๋ฉฐ ๊ทธ ๊ฐ์ ๋ชจ๋ ๋ํด์ฃผ์ด ์ถ๋ ฅํ๋ค.
์๋ ์ฝ๋
์๊ฐ๋ณด๋ค ๊ฐ๋จํด์ ๋ค์ ์คํ ๋ฌธ์ ๋ ๋ ์ด๋ ค์ด ๋ฌธ์ ๋ก ๊ฐ์ ธ์ฌ๊ฒ์ฅ..^<^
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ