2021-06-20 · 1분
BOJ 11047 — Coin 0
A short write-up for BOJ 11047 (Coin 0), a greedy coin problem.
2021-06-03 · 3분
This post is over 2 years old. The content may be outdated.
BOJ #10828 stack problem: https://www.acmicpc.net/problem/10828
A stack is a list structure with restricted access. It's a LIFO (Last In First Out) data structure where data can be inserted and removed only from one end.

There are several, but these are the most commonly used.
Depending on the type of problem, storing data in a stack rather than an array may be a more suitable method.
BOJ #10828 stack problem

#include<stdio.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define MINUS -1
#define MAX_SIZE 10000
typedef struct _stack{
int arr[MAX_SIZE];
int top;
} Stack;
void StackInit(Stack * sp){
sp->top = -1;
}
int IsEmpty(Stack * sp){
if(sp->top == -1) return TRUE;
return FALSE;
}
int Size(Stack *sp){
return sp->top + 1;
}
int IsFull(Stack * sp){
if(sp->top + 1 >= MAX_SIZE) return TRUE;
return FALSE;
}
void Push(Stack * sp, int data){
if(IsFull(sp)==TRUE) return;
sp->arr[++(sp->top)] = data;
}
int Pop(Stack * sp){
if(IsEmpty(sp) == TRUE) return MINUS;
return sp->arr[(sp->top)--];
}
int Peek(Stack *sp){
if(IsEmpty(sp) == TRUE) return MINUS;
return sp->arr[sp->top];
}
int main(void){
int i;
char str[6];
Stack stack;
int n, num;
scanf("%d", &n);
fgetc(stdin);
StackInit(&stack);
for(i=0; i<n; i++){
scanf("%s", str);
fgetc(stdin);
if(!strcmp(str, "push")){
scanf("%d", &num);
fgetc(stdin);
Push(&stack, num);
}else if(!strcmp(str, "pop")){
printf("%d\n", Pop(&stack));
}else if(!strcmp(str, "empty")){
printf("%d\n", IsEmpty(&stack));
}else if(!strcmp(str, "size")){
printf("%d\n", Size(&stack));
}else if(!strcmp(str, "top")){
printf("%d\n", Peek(&stack));
}
}
return 0;
}
You can make and use it directly like this, but C++ has a header called stack.
This isn't the answer to the problem above; it's just how you can do it when using the stack header.
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
stack<int> Stack;
int main()
{
int i;
char str[7];
int n, num;
scanf("%d", &n);
fgetc(stdin);
for (int i = 0; i < n; i++)
{
scanf("%s", str);
fgetc(stdin);
if(!strcmp(str, "push")) {
scanf("%d", &num);
fgetc(stdin);
Stack.push(num);
}else if(!strcmp(str, "pop")) {
if(Stack.empty()) printf("-1\n");
else {
printf("%d\n", Stack.top());
Stack.pop();
}
}else if(!strcmp(str, "empty")) {
if(Stack.empty()) printf("1\n");
else printf("0\n");
}else if(!strcmp(str, "size")) {
printf("%ld\n", Stack.size());
}else if(!strcmp(str, "top")) {
printf("%d\n", Stack.top());
}
}
}
// add the stack header
#include <stack>
// create an empty stack
stack<int> Stack;
// create a stack initialized to {1, 2, 3, 4, 5}
stack<int> Stack({ 1, 2, 3, 4, 5 });
Based on stack<int> Stack
Problem solving using a stack:
**[Algorithm] - BOJ #9012 parentheses**](https://gmlwjd9405.github.io/2018/08/03/data-structure-stack.html) Reference: https://ko.wikipedia.org/wiki/%EC%8A%A4%ED%83%9D https://velog.io/@choiiis/C-STL-stack-%ED%81%B4%EB%9E%98%EC%8A%A4-%EC%A0%95%EB%A6%AC [https://gmlwjd9405.g — data-structure-stack.html
Image: http://www.incodom.kr/%EC%8A%A4%ED%83%9D
Original (Korean): tistory — published 2021-06-03, migrated to this blog. This translation was generated with the help of AI.
…