백준 알고리즘 10773번 : 제로
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int k;
int num;
int* stack;
int idx = 0;
scanf("%d", &k);
stack = (int*)malloc(sizeof(int) * k);
for (int i = 0; i < k; i++)
{
scanf("%d", &num);
if (num == 0)
idx--;
else
stack[idx++] = num;
}
num = 0;
for (int i = 0; i < idx; i++)
num += stack[i];
free(stack);
printf("%d", num);
}
백준 알고리즘 9012번 : 괄호
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int t;
int idx = 0;
scanf("%d", &t);
int* arr = (int*)malloc(sizeof(int) * t);
for (int i = 0; i < t; i++)
{
char str[51] = { 0, };
scanf("%s", str);
int idx = 0;
for (int j = 0; str[j]; j++)
{
if (str[j] == '(')
idx++;
else if (str[j] == ')')
idx--;
if (idx < 0)
break;
}
if (idx == 0)
arr[i] = 1;
else
arr[i] = 0;
}
for (int i = 0; i < t; i++)
{
if (arr[i] == 1)
printf("YES");
else
printf("NO");
if (i != t - 1)
printf("\n");
}
free(arr);
return (0);
}
백준 알고리즘 18258번 : 큐 2
#include <stdio.h>
#include <string.h>
int queue[2000001];
int front = 0;
int rear = -1;
void push(int x)
{
queue[++rear] = x;
}
void pop()
{
if (rear - front + 1 == 0)
printf("-1\n");
else
printf("%d\n", queue[front++]);
}
void empty()
{
if (rear - front + 1 != 0)
printf("0\n");
else
printf("1\n");
}
int main(void)
{
int n, t, x;
char order[6];
scanf("%d", &n);
for (t = 0; t < n; t++)
{
scanf("%s", order);
if (!strcmp(order, "push"))
{
scanf("%d", &x);
push(x);
}
else if (!strcmp(order, "pop"))
pop();
else if (!strcmp(order, "size"))
printf("%d\n", rear - front + 1);
else if (!strcmp(order, "empty"))
empty();
else if (!strcmp(order, "front"))
{
if (rear - front + 1 == 0)
printf("-1\n");
else
printf("%d\n", queue[front]);
}
else if (!strcmp(order, "back"))
{
if (rear - front + 1 == 0)
printf("-1\n");
else
printf("%d\n", queue[rear]);
}
for (int j = 0; j < 6; j++)
order[j] = 0;
}
return 0;
}