• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[백준] 1158번 : 조세퍼스문제 with cpp

17 Aug 2018

Reading time ~1 minute

문제 : https://www.acmicpc.net/problem/1158

#include<iostream>

using namespace std;

class Node
{
public:
    Node* next;
    int value;
};

class CircularList
{
    Node* head;
    Node* tail;

public:
    CircularList(int N)
    {
        head = new Node();
        head->value = 1;

        Node* tmp_Node = head;
        for(int i=2;i<N+1;i++)
        {

            Node *n = new Node();
            n->value = i;
            tmp_Node->next = n;
            tmp_Node = n;
        }
        tail = tmp_Node;
        tail->next=head;

    }
    int pop(int M)
    {
        for (int i = 0; i<M-1; i++) {
            head = head->next;
            tail = tail->next;
        }

        int rt = head->value;

        Node*tmp = head;
        tail->next=head->next;
        head = tail->next;
        delete tmp;


        return rt;
    }
};
int main()
{

    int N,M;
    cin>>N>>M;

    CircularList cl = CircularList(N);

    cout<<"<";
    while(N--)
    {
        cout<<cl.pop(M);
        if(N != 0) cout<<", ";
    }
    cout<<">"<<endl;
    return 0;
}




algorithm백준 Share Tweet +1