约 瑟 夫 环 的 中心语 句
一题C程序,高手进来
答案:5 悬赏:0 手机版
解决时间 2021-07-16 21:30
- 提问者网友:兔牙战士
- 2021-07-15 23:33
最佳答案
- 五星知识达人网友:摆渡翁
- 2021-07-16 00:22
int i,j;
int s1,w;
s1=s;
for (i=0;i<n;i++)
p[i]=i+1;
for (i=n;i>=2;i--) 或 for (i=n;i>1;i--)
{ s1=(s1+m-1)%i;
if (s1==0)
s1=i;
w=p[s1-1];
for (j=s1;j<i;j++) 或 for (j=s1-1;j<=i-1;j++)
p[j-1]=p[j];
p[i-1]=w;
}
全部回答
- 1楼网友:愁杀梦里人
- 2021-07-16 04:22
莫非楼主求的是传说中的约瑟夫数学公式?见下面公式,比什么循环链表循环数组简单多了。
令f[i]表示i个人报m退出最后留下者的编号,求f[n]
递推公式: f[1]=0; f[i]=(f[i-1]+m)%i; (i>1)
- 2楼网友:像个废品
- 2021-07-16 03:01
你学过c++没?学了我用c++编
- 3楼网友:佘樂
- 2021-07-16 01:30
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int m,h;
struct node *next;
}node,*linklist;
linklist createform()
{
linklist L;
node *r,*s;
int flag=1,i=1,c;
L=(node*)malloc(sizeof(node));
L->next=NULL;
r=L;
printf("请输入第%d个人的密码",i);
scanf("%d",&c);
if(c!=0)
{
L->m=c;
L->h=i;
}
i=2;
while(flag)
{ printf("请输入第%d个人的密码",i);
scanf("%d",&c);
if(c!=0)
{
s=(node*)malloc(sizeof(node));
s->m=c;
s->h=i;
r->next=s;
r=s;
i++;
}
else
{
flag=0;
r->next=L;
}
}
return L;
}
void outline(linklist L)
{
int a,data,flag=1;
node *r,*s;
printf("输入起始密码");
scanf("%d",&data);
data=data-1;
r=L;
while(r->next!=NULL)
{
for(a=1;a<data;a++)
{
r=r->next;
}
s=r->next;
r->next=s->next;
data=s->m;
printf(" 第%d个人的密码 %d \n",s->h,s->m);
free(s);
}
}
main()
{
linklist l;
l=createform();
outline(l);
全给你了,要给分呀
- 4楼网友:封刀令
- 2021-07-16 00:45
我前不久刚做完,用链表做的
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯