### C++经典题目一：约瑟夫问题

```//====================================================================
//				this program resolved a Josephus loop problem
//				Achieved by a loop-list
//				if you have any questions, welcome to chat with me
//				QQ：88521134	Email:88521134@qq.com
//====================================================================
#include "stdafx.h"
#include <iostream>
using namespace std;

typedef struct node
{
int data;
node* next;
}node;

//========================================================================
// node* loop_create(int a[], int n):	use an array to create a loop-list
// int a[]:	array
// int n:	the number of elements in the array
//========================================================================
node* loop_create(int a[], int n)
{
int i;
node *p,*q, *pre;
pre = (node*)malloc(sizeof(node));
pre->data = a[0];
for (i=1; i<n; i++)
{
p = (node*)malloc(sizeof(node));
p->data = a[i];
if (i==1)
pre->next = p;
else
q->next = p;
q=p;
}
q->next = pre;
return pre;

}

//====================================
//	test program: Josephus Problem
//====================================
int _tmain(int argc, _TCHAR* argv[])
{
node* loopList = NULL;
int a[5]={4,2,3,5,6};
int n = 5;
int m = 7;
int i;

loopList = loop_create(a,5);	// 创建一个循环链表
node *p=NULL;					//下面开始循环取数
p=loopList;
m%=n;
while (p!=p->next)
{
for (i=1; i<m-1;i++)
{
p=p->next;
}
m=p->next->data;
printf("%d", m);
m%=(--n);
p->next = p->next->next;
p=p->next;
}
printf("%d\n",p->data);
delete[]loopList;
system("pause");
return 0;
}
```