티스토리 뷰

1. 문제

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

 

 

2. 풀이

#include <stdio.h>
#include <stdlib.h>

#define SIZE 1000

typedef struct
{
    int data[SIZE];
    int front, rear;
} QueueType;

void init(QueueType *Q)
{
    Q->front = Q->rear = -1;
}

int isEmpty(QueueType *Q)
{
    return Q->front == Q->rear;
}

int isFull(QueueType *Q)
{
    return Q->rear == SIZE-1;
}

void enqueue(QueueType *Q, int e){
    if(!isFull(Q)){
        Q->rear++;
        Q->data[Q->rear] = e;
    } else {
        return -1;
    }
}

int dequeue(QueueType *Q){
    if(!isEmpty(Q)){
        Q->front++;
        return Q->data[Q->front];
    } else {
        return -1;
    }
}

int main(void){
    while(1){
        int total = 0;
        int length = 0;
        int digit;
        scanf("%d", &digit);

        if(digit == -1){ break; }

        QueueType resultQ;
        init(&resultQ);

        for(int i = 1; i < digit; i++){
            if(digit % i == 0){
                enqueue(&resultQ, i);
                length ++;
                total += i;
            }
        }

        if(total == digit){
            printf("%d = ", digit);
            for(int i = 0; i < length; i ++){
                if(i != length-1){
                    printf("%d + ", dequeue(&resultQ));
                } else {
                    printf("%d\n", dequeue(&resultQ));
                }
            }
        } else {
            printf("%d is NOT perfect.\n", digit);
        }
    }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함