// 123f.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct que{
que* pre;
que* next;
int data;
}DOUCIRLQUE;
void Joseph(int n,int m,int k)//n个人 m个数 k个人开始
{
DOUCIRLQUE *first;
DOUCIRLQUE *tmp;
DOUCIRLQUE *tmppre;
int len=n;
first = new DOUCIRLQUE();
first->data = 1;
tmppre = first;
for(int i=1;i<n;i++)
{
tmp = new DOUCIRLQUE();
tmppre->next = tmp;
tmp->data = i+1;
tmp->pre = tmppre;
tmppre = tmp;
}
tmp->next = first;
first->pre = tmp;
tmp = first;
/**< for(int i=0;i<2*n;i++)
{
cout<<tmp->data<<endl;
tmp=tmp->next;
} */
tmp = first;
for(int i=1;i<k;i++)
{
tmp=tmp->next;
}
while(len != 1)
{
for(int i=0;i<m-1;i++)
{
tmp=tmp->next;
}
// cout<<tmp->data<<" "<<endl;
tmp->pre->next = tmp->next;
tmp->next->pre = tmp->pre;
tmppre = tmp;
cout<<tmppre->data<<endl;
tmp = tmp->next;
delete tmppre;
len--;
}
cout<<tmp->data<<endl;
delete tmp;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n =5 ;
Joseph(10,5,1);
cout << "Hello world!" << endl;
return 0;
}
约瑟夫环的实现
最新推荐文章于 2024-09-03 11:06:17 发布