#ifndef LNODE_H
#define LNODE_H
#define ERROR 0
#define OK 1
#define MAX 1000
int Key[MAX];
int n;
typedef struct LNode{
int keyword, number;
struct LNode *next;
}LNode, *LinkList;
void JosephC(LinkList p, int m, int x){
LinkList q;
int i;
if(x != 0){
q = p;
m %= x;
if(m == 0) m = x;
for(i = 1; i <= m; i++){
p = q;
q = p -> next;
}
p -> next = q -> next;
i = q -> keyword;
printf("%d ", q -> number + 1);
free(q);
JosephC(p, i, x-1);
}
}
#endif
#include<stdio.h>
#include<stdlib.h>
#include"LNode.h"
int main(){
int i, m, j;
LinkList Lhead, p, q;
printf("输入初值m:\n");
scanf("%d", &m);
printf("输入人数n:\n");
scanf("%d", &n);
printf("依次输入%d个人的密码:\n", n);
for(j = 0; j < n; j++){
scanf("%d", Key + j);
}
Lhead = (LinkList)malloc(sizeof(LNode));
if(!Lhead)
return ERROR;
Lhead -> keyword = Key[0];
Lhead -> next = NULL;
Lhead -> number = 0;
p = Lhead; //初始化头结点
for(i = 1; i < n; i++){
if(!(q = (LinkList)malloc(sizeof(LNode))))
return ERROR;
q -> keyword = Key[i];
q -> number = i;
p -> next = q;
p = q;
}
p -> next = Lhead;
printf("其出列顺序为:\n");
JosephC(p, m, n);
printf("\n");
return OK;
}
#define LNODE_H
#define ERROR 0
#define OK 1
#define MAX 1000
int Key[MAX];
int n;
typedef struct LNode{
int keyword, number;
struct LNode *next;
}LNode, *LinkList;
void JosephC(LinkList p, int m, int x){
LinkList q;
int i;
if(x != 0){
q = p;
m %= x;
if(m == 0) m = x;
for(i = 1; i <= m; i++){
p = q;
q = p -> next;
}
p -> next = q -> next;
i = q -> keyword;
printf("%d ", q -> number + 1);
free(q);
JosephC(p, i, x-1);
}
}
#endif
#include<stdio.h>
#include<stdlib.h>
#include"LNode.h"
int main(){
int i, m, j;
LinkList Lhead, p, q;
printf("输入初值m:\n");
scanf("%d", &m);
printf("输入人数n:\n");
scanf("%d", &n);
printf("依次输入%d个人的密码:\n", n);
for(j = 0; j < n; j++){
scanf("%d", Key + j);
}
Lhead = (LinkList)malloc(sizeof(LNode));
if(!Lhead)
return ERROR;
Lhead -> keyword = Key[0];
Lhead -> next = NULL;
Lhead -> number = 0;
p = Lhead; //初始化头结点
for(i = 1; i < n; i++){
if(!(q = (LinkList)malloc(sizeof(LNode))))
return ERROR;
q -> keyword = Key[i];
q -> number = i;
p -> next = q;
p = q;
}
p -> next = Lhead;
printf("其出列顺序为:\n");
JosephC(p, m, n);
printf("\n");
return OK;
}