2 回答
TA贡献1871条经验 获得超13个赞
参考代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
int number;
struct LNode *next;
} LNode,*Linklist;
int main()
{
int M,N;
int i;
Linklist L,r;
printf("Please input two M , N:");
scanf("%d%d",&M,&N);
L = (Linklist)malloc(N * sizeof(struct LNode));
if(L == NULL) printf("Error!\n"),exit(0);
r = L;
for(i = 1;i < N;i++)
{
r->next = L + i;
r->number = i;
r = r->next;
}
r->next = L;
r->number = N;
while(r != r->next)
{
for(i = 0;i < M - 1;i++)
r = r->next;
printf("%4d",r->next->number);
r->next = r->next->next;
}
printf("\n When N = %d and M = %d,Josephus,hiding in position %d survive.\n",N,M,r->number);
free(L);
}
- 2 回答
- 0 关注
- 398 浏览
添加回答
举报