# 设有n个人围坐在一个圆桌周围，现从第s个人开始报数，数到第m的人出列，然后从出列的下一个人重新开始报数，数到m的人又出列，如此重复，直到所有的人全部出列为止。Josephus问题是：对于任意给定


#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <malloc.h>

using namespace std;
typedef struct list//链表
{
int date;
struct list *next;
list* l;
{
l->next = l;
l->date = 1;
for (int i = n; i > 1; i--)
{
p->next = l->next;
l->next = p;
p->date = i;

}

}
void out(linklist l, int s, int m,int n)//依次出列
{
list* p = l;
int i ;
for (i = 1;; i++)//寻找s的位置
{
if (p->date == s)
{
break;
}
p = p->next;

}//找出s的位置 p记录

if (m != 1)//当m不等于一时
{
for (int j = 1; j <= n; j++)//有n次出列动作
{
if (j == 1)
{
for (int h = 1; h < m - 1; h++)
{
p = p->next;
}
}
else
{
for (int h = 1; h < m; h++)
{
p = p->next;
}
}
cout << p->next->date << " ";
p->next = p->next->next;
}
}
else
{
for (int j = 1; j <= n; j++)
{
cout << p->date << " ";
p = p->next;
}
}

}
int main()
{
while (1)
{
int n, s, m;
cout << "依次输入n s m:";
cin >> n >> s >> m;
if (n < s||n<=0||s<=0||m<=0)
{
cout << "输入错误,请重试!" << endl;
continue;
}
intlist(l, n);
cout << "输出排序:";
out(l, s, m, n);
cout << "\n";
}

return 0;
}


03-13 831
07-17 4816
09-29 705
03-09 1659