GCC 컴파일러 관련하여 질문드립니다. (JOSEPHUS)

  • Asterix
    Asterix

    문제 : JOSEPHUS

    안녕하세요. 알고스팟에서 알고리즘을 열심히 공부하고 있는 학생입니다. 다름이 아니라, 아래와 같이 짠 소스를 GCC 컴파일러를 통해 채점할 경우 오답으로 분류되어 여러 고민을 하였음에도 해답을 찾지 못해 이렇게 글을 올리게 되었습니다.

    자주하는 실수 모음과 유의 사항등을 읽었음에도 문제를 찾기가 어렵네요. 조언 부탁드립니다!

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BOOL char
    #define TRUE 1
    #define FALSE 0
    
    int n, k;
    
    typedef struct Node Node;
    struct Node{
        int element;
        Node *prev;
        Node *next;
    };
    
    Node *head;
    
    void Initlist()
    {
        head = (Node *)malloc(sizeof(Node));
        head->prev = NULL;
        head->next = NULL;
    }
    
    Node *insertNode(Node *Target, Node *aNode)
    {
        Node *New;
        Node *Right;
    
        New = (Node *)malloc(sizeof(Node));
        *New = *aNode;
    
        Right = Target->next;
        New->next = Right;
        New->prev = Target;
        Target->next = New;
        if (Right) {
            Right->prev = New;
        }
        return New;
    }
    
    
    BOOL DeleteNode(Node *Target)
    {
        Node *Left, *Right;
    
        if (Target == NULL || Target == head) {
            return FALSE;
        }
    
        Left = Target->prev;
        Right = Target->next;
    
        Left->next = Right;
        Right->prev = Left;
        head->next = Right;
        free(Target);
    
        return TRUE;
    }
    
    void UnInitList()
    {
        while (DeleteNode(head)) { ; }
    
        free(head);
        head = NULL;
    }
    
    void input()
    {
        scanf("%d %d", &n, &k);
    }
    
    void process()
    {
        int i, count, countnum = 0;
        Node *Now, Temp;
    
        // 리스트 초기화
        Initlist();
    
        count = n;
        Now = head; 
        // n만큼 노드 삽입(시계 방향)
        for (i = 1; i <= n; i++)
        {
            Temp.element = i;
            Now = insertNode(Now, &Temp);
    
            if (i == n)
            {
                Now->next = head->next;
                Now->next->prev = Now;
            }
        }
    
        DeleteNode(head->next);
        count--;
        while (count != 2)
        {
            for (Now = head->next; Now; Now = Now->next)
            {
                if (countnum == k - 1)
                {
                    DeleteNode(Now);
                    break;
                }
                countnum++;
            }
            countnum = 0;
            count--;
        }
    }
    
    void output()
    {
        printf("%d %d ", head->next->element, head->next->next->element);
        UnInitList();
    }
    
    int main()
    {
        int t;
        //freopen("input.txt", "r", stdin);
        scanf("%d", &t);
    
        while (t--)
        {
            input();
            process();
            output();
        }
        return 0;
    }
    

    9년 전
2개의 댓글이 있습니다.
  • Being
    Being

    얼핏 살펴보기로는 각 테스트 케이스마다 한 줄에 두 개의 정수를 출력해야 하는데 그렇게 하지 않으시는 것으로 보입니다.


    9년 전 link
  • Asterix
    Asterix

    Being님 답변 감사합니다. 출력부에서 한 줄에 두 개의 정수를 출력해보았으나 역시 오답으로 처리되더군요. 다시 한번 짜봐야 할 것 같아요. ㅎㅎ


    9년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.