0
点赞
收藏
分享

微信扫一扫

5_3_天天向上


数码管 光照度 温湿度 传感器测试
网口 UART测试
步进电机 强上拉电阻
STC芯片手册

#include<stdio.h>

#include<malloc.h>

int Josephu(int n,int m)

{

int flag,i,j=0;

int *arr=(int *)malloc(n*sizeof(int));

for(i=0;i<n;++i)

arr[i]=1;

for(i=1;i<n;++i)

{

flag=0;

while(flag<m)

{

if(j==n)

j=0;

if(arr[j])

++flag;

++j;

}

arr[j-1]=0;

printf("第%4d个出局的人是:%4d号\n",i,j);

}

free(arr);

return j;

}

int main()

{

int n,m;

scanf("%d%d",&n,&m);

printf("最后胜利的是%d号!\n",Josephu(n,m));

system("pause");

return 0;

}

------------------------------------------------------------

//链表实现:

#include<stdio.h>

#include<malloc.h>

typedef struct Node

{

int index;

struct Node *next;

}JosephuNode;

int Josephu(int n,int m)

{

int i,j;

JosephuNode *head, *tail;

head=tail=(JosephuNode *)malloc(sizeof(JosephuNode));

for(i=1;i<n;++i)

{

tail->index=i;

tail->next=(JosephuNode *)malloc(sizeof(JosephuNode));

tail=tail->next;

}

tail->index=i;

tail->next=head;



for(i=1;tail!=head;++i)

{

for(j=1;j<n;++j)

{

tail=head;

head=head->next;

}

tail->next=head->next;

printf("第%4d个出局的人是:%4d号\n",i,head->index);

free(head);

head=tail->next;

}

i=head->index;

free(head);

return i;

}

int main()

{

int n,m;

scanf("%d%d",&n,&m);

printf("最后胜利的是%d号!\n",Josephu(n,m));

system("pause");

return 0;

}

举报

相关推荐

0 条评论