View Code of Problem 80

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int n;
    node* next;
}*linklist ;

int main(){
    int n;
    while(cin>>n){
        if(n==0){
            break;
        }
        node *head = new node;
        head->next = NULL; 
        node *p = head;
        for(int i= 1; i<=n; i++){
            node *q = new node;
            q->n = i;
            q->next = NULL;
            p->next = q;
            p = p->next;
        }
        p->next  = head->next;
        int cnt = 0;
        p = head->next;
        while(p->next!=p){
            cnt++;
            if(cnt==2){
                cnt=0;
                p->next = p->next->next;
                p = p->next;
            }
            else{
                p = p->next;
            }
        }
        cout<<p->n<<endl;
    }

    return 0;
}

Double click to view unformatted code.


Back to problem 80