约瑟夫问题!!!调试不通

muhao 2003-10-15 03:47:12
链表实现的约瑟夫问题

//链表式约瑟夫问题
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
typedef struct node_type{
int seq,code;
struct node_type *next;
}link;
main()
{
int i,m,n,cn;
link *h,*p,*q;
printf("Input the first code m:");
scanf("%d",&m);//输入初始密码
printf("Input number n:");
scanf("%d",&n);//输入人数
m=m-1;
p=(link*)malloc(sizeof(link));
h=p; q=p;
printf("Input codes:");
for(i=1;i<=n-1;i++)
{
scanf("%d",&cn);
p->seq=i;p->code=cn;
p=(link*)malloc(sizeof(link));
q->next=p; q=p;
}
scanf("%d",&cn);
p->seq=n;
p->code=cn;
p->next=h;
if((n>1)&&(m==0))
{
m=h->code;printf("%d ",h->seq);
p->next=h->next;
free(h);
h=p->next;
}
p=h;
while((n>1)&&(m!=0))
{
if(m>1) for(i=1;i<=m-1;i++)p=p->next;
q=p->next;
m=q->code;
printf("%d ",q->seq);
p->next=q->next;
free(q);
n=n-1;
}
printf("%d\n",p->seq);
getch();
}
...全文
71 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky_apple 2003-10-19
  • 打赏
  • 举报
回复
可以呀!!我在DEV C++中能通过编译的!不知道你用的是什么编译器呀
lipeijie492728 2003-10-19
  • 打赏
  • 举报
回复
代码看懂了,可不知“约瑟夫问题“具体是干啥的!
lipeijie492728 2003-10-19
  • 打赏
  • 举报
回复
楼上的?是要干什么呀?
playboyxp 2003-10-19
  • 打赏
  • 举报
回复
#include <iostream.h>
void main()
{
int a[17],i,j,k=1,integer;
for(i=0;i<17;i++)
a[i]=i+1;
cin>>integer;
i=-1;
while(1)
{
for(j=0;j<integer;)
{
i=(i+1)%17;
if(a[i]!=0) j++;
}
if(k==17) break;
cout<<a[i]<<" ";
a[i]=0;
k++;
}
cout<<a[i]<<endl;
}
muhao 2003-10-16
  • 打赏
  • 举报
回复
呵呵,不好意思,由于输入错误。
iwillwin 2003-10-15
  • 打赏
  • 举报
回复
讲一讲报告的错误或者运行的错误是什么
李马 2003-10-15
  • 打赏
  • 举报
回复
我把你的程序粘了下来,编译运行结果如下,完全正确:

Input the first code m:20
Input number n:7
Input codes:3
1
7
2
4
8
4
6 1 4 7 2 3 5

我的环境是XP + VC6。

69,335

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧