본문 바로가기
ⓒⓞⓓⓘⓝⓖⓣⓔⓢⓣ/ⓒ

[백준/C언어] 도키도키 간식드리미

by heaven00 2024. 8. 8.
728x90

📌 문제 링크

https://www.acmicpc.net/problem/12789

 

간단한 문제인데 while문의 코드를 else if로 만들어서 넣었다가 틀려서 바꿨다!

 

👩‍💻 전체코드

#include <stdio.h>
int N;
int arr[1000+10];
int stack[1000+10];

int front = -1;
void push(int n) {
    stack[++front] = n;
}

int pop() {
    return stack[front--];
}

int top() {
    return stack[front];
}

void input() {
    scanf("%d", &N);
    for(int i = 0; i<N; i++) {
        scanf("%d", &arr[i]);
    }
}

void solve() {
    int queue = 1;
    for(int i = 0; i<N; i++) {
        if(queue == arr[i]) {
            queue += 1;
            while(queue == top() && front >= 0) {
                pop();
                queue += 1;
            }
        } else {
            push(arr[i]);
        }
    }
    if(front != -1) {
        printf("Sad");
    } else {
        printf("Nice");
    }
}

int main() {
    input();
    solve();
    return 0;
}
728x90

댓글