#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
//定义Joseph的节点结构体
typedef struct josephnode
{
int joseph_num;
int joseph_password;
}joseph_node;
//输入各个结点信息的函数
void joseph_input(joseph_node arry[],int n,int *begin_num);
//处理函数
void joseph_process(joseph_node arry[],int n,int *begin_num);
void main()
{
int total_pers;
int begin_num=0;
joseph_node * ip_jose;
puts("please enter the total persons:");
scanf("%d",&total_pers);
ip_jose=(joseph_node *)malloc((total_pers*sizeof(joseph_node)));
if(!ip_jose)
exit(1);
joseph_input(ip_jose,total_pers,&begin_num);
joseph_process(ip_jose,total_pers,&begin_num);
}
void joseph_input(joseph_node arry[],int n,int *begin_num)
{
int i;
int password;
printf("please enter the begin password:\n");
scanf("%d",begin_num);
for(i=0;i<n;i++)
{
printf("please enter th%d person's information:\n",i+1);
printf("please enter the person's password:\n");
scanf("%d",&password);
fflush(stdin);
arry[i].joseph_num=i+1;
arry[i].joseph_password=password;
}
}
void joseph_process(joseph_node arry[],int n,int *begin_num)
{
int ip;
int counter=0;
int total_num;
int j;
int count;
int ip_node;
int password;
total_num=n;
ip=0;
password=*begin_num;
//就一个结点的情形
if(total_num==1)
{
printf("th%d person out of the line is :\t",++counter);
printf("%d\n",arry[0].joseph_num);
exit(1);
}
//多个接点的情形
do{
count=0;
do{
count++;
ip_node=ip%total_num;
ip++;
}while(count<password);
//输出出列的那个
printf("th%d person out of the line is :\t",++counter);
printf("%d\n",arry[ip_node].joseph_num);
//删除那个接点重新组合数组
password=arry[ip_node].joseph_password;
ip=(ip-1)%total_num;
total_num=total_num-1;
for(j=ip;j<total_num;j++)
{
//arry[j].joseph_num=arry[j+1].joseph_num;
//arry[j].joseph_password=arry[j+1].joseph_password;
arry[j]=arry[j+1];
}
}while(total_num>1);
//最后一个接点输出
printf("th%d person out of the line is :\t",++counter);
printf("%d\n",arry[0].joseph_num);
}