링크 :링크텍스트
링크 : 링크텍스트
#include <iostream>
#include <stack>
using namespace std;
int main(void)
{
int k, n;
cin >> k;
stack<int>s;
for (int i = 0; i < k; i++)
{
cin >> n;
if (n != 0) {
s.push(n);
}
else {
s.pop();
}
}
int sum = 0;
int size = s.size();
for (int i = 0; i < size; i++)
{
sum += s.top();
s.pop();
}
cout << sum;
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int queue[10000];
int queue_size = 0;
void push(int push_data)
{
queue[queue_size] = push_data;
queue_size += 1;
}
int empty() {
if (queue_size == 0) {
return 1;
}
else {
return 0;
}
}
int pop()
{
if (empty()) {
return -1;
}
queue_size -= 1;
return queue[0];
}
int front()
{
if (empty()) {
return -1;
}
return queue[queue_size - queue_size];
}
int back()
{
if (empty()) {
return -1;
}
return queue[queue_size - 1];
}
void setting()
{
for (int i = 0; i < queue_size; i++) {
queue[i] = queue[i + 1];
}
}
int main(void)
{
int N = 0, push_data = 0;
char command[5] = { 0, };
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%s",command);
if (!strcmp(command, "push")) {
scanf("%d", push_data);
push(push_data);
}
else if (!strcmp(command, "pop")) {
printf("%d\n", pop());
setting();
}
else if (!strcmp(command, "empty")) {
printf("%d\n", empty());
}
else if (!strcmp(command, "size")) {
printf("%d\n",queue_size);
}
else if (!strcmp(command, "front")) {
printf("%d\n", front());
}
else if (!strcmp(command, "back")) {
printf("%d\n", back());
}
}
return 0;
}
2) k 번 반복하는 반복문으로 k 개의 수를 받는다.
3) 이때 입력받은 수가 0이면 pop를 실행하고 0이 아니면 push를 실행한다.
4) 스택의 모든 값을 더한다.
5) cout << sum; // 총합을 출력한다.
1) cin >> N; // 정수 N을 입력받는다.
2) N 번 반복하는 반복문 내에서 조건문을 통해 명령어(push, pop 등)를 입력받는다.
3) 입력받은 명령어에 따라 c++에서 미리 구현된 큐의 명령어를 실행시킨다.