约瑟夫

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;


struct Node{
    int id;
    Node *next;
};

int main()
{
    Node *node,*head,*last;
    head = NULL;
    for(int i = 0;i < 21;i++){
        node = new Node;
        node->id = i + 1;
        if(head == NULL)
            head = last = node;
        else{
            last->next = node;
            last = node;
        }
    }
    last->next = head;
    int count = 1,p = 17;
    while(head != NULL){
        if(head->next == head){
            printf("%d",head->id);
            delete head;
            break;
        }
        if(count == p - 1){
            node = head->next;
            head->next = node->next;
            delete node;
            count = 0;
        }
        head = head->next;
        count++;
    }
    return 1;
}



你可能感兴趣的:(约瑟夫)