程序运行???

miraches 2006-10-04 11:52:43
约瑟夫问题 有n个人围成一圈,1到3开始转圈报名,喊道3的同学出列,问最后剩下原来的几号
#include<stdio.h>
#include<malloc.h>
main()
{
int i,j=0,a,*p,x=-1,k=0;
printf("please input one NUM\n");
scanf("%d",&a);
p=(int *)calloc(a,sizeof(int));
*p=1;
for(i=2;i<=a;i++)
*(p+i)=i;
while(1)
{
x++;
if(x>=a) x-=a;
if(*(p+x)!=0) j++;
if(j%3==0&&*(p+x)!=0)
{printf("%d\t",*(p+x));
*(p+x)=0;
j=0;
k++;
if(k==a) break;
}
}
}
程序不能退出....晕....
请高手指教!!!
...全文
287 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
abcwangdragon 2006-10-11
  • 打赏
  • 举报
回复
用数组实现历害,
chinese_zmm 2006-10-11
  • 打赏
  • 举报
回复
学习
dandanmylady 2006-10-09
  • 打赏
  • 举报
回复
mark
laiwusheng 2006-10-09
  • 打赏
  • 举报
回复
/*忘了主函数*/
main()
{
int lq[13];
joseph(lq,13,3);
getch();
}
laiwusheng 2006-10-09
  • 打赏
  • 举报
回复
/*我给你用C语言写了一个
TC下成功
思想很简单没用到复杂的数据结构
*/
#include <stdio.h>
#include <conio.h>
void joseph(int L[],int n,int m)
{
int i,j,t,s=1;
for(i=1;i<=n;i++)
L[i]=i;
for(i=n;i>1;i--){

s=(s+m-1)%i;
if(s==0)
s=i;
t=L[s];
for(j=s;j<=i-1;j++)
L[j]=L[j+1];
L[i]=t;
}
for(i=1;i<=n;i++)
printf("%3d",L[i]);
}
shone_sun 2006-10-09
  • 打赏
  • 举报
回复
用循环链表 简单点
jixingzhong 2006-10-09
  • 打赏
  • 举报
回复
/*约瑟夫环*/

#include <stdlib.h>
#include <stdio.h>
typedef struct node
{
int data;
struct node *next;
}LNode;

main()
{
LNode* Create(int,int);
LNode* GetNode(LNode *);
int Print(LNode *,int);
LNode *p;
int n,k,m;
do
{
printf ("输入总人数");
scanf ("%d",&n);
}
while (n<=0);
do
{
printf ("输入开始人的序号(1~%d)",n);
scanf ("%d",&k);
}
while (k<=0 || k>n);
do
{
printf ("输入间隔数字");
scanf ("%d",&m);
}
while(m<=0);

p=Create(n,k);
Print(p,m);
return 0;
};

LNode* Create(int n,int k)/*创建循环链表*/
{
int start=k-1;
LNode *s,*p,*L=0,*t;
if (start==0) start=n;
while (n!=0)
{
s=(LNode *)malloc(sizeof(LNode));
if (L==0) p=s;
if (n==start) t=s;
s->data=n;
s->next=L;
L=s;
n--;
}
p->next=L;
return t;
}

LNode* GetNode(LNode *p)/*出队函数*/
{
LNode *q;
for (q=p;q->next!=p;q=q->next);
q->next=p->next;
free (p);
return (q);
}

Print(LNode *p,int m)/*输出函数*/
{
int i;
printf ("出队编号:\n");
while (p->next!=p)
{
for (i=1;i<=m;i++)
p=p->next;
printf ("%d ",p->data);
p=GetNode(p);
}
printf("%d\n",p->data);
return 0;
}
飞哥 2006-10-09
  • 打赏
  • 举报
回复
约瑟夫环
miraches 2006-10-08
  • 打赏
  • 举报
回复
谢谢楼上大哥!
itisyu 2006-10-05
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<malloc.h>
main()
{
int i,j=0,a,*p,x=-1,k=0;
printf("please input one NUM\n");
scanf("%d",&a);
p=(int *)calloc(a,sizeof(int));
*p=1;
for(i=1;i<=a;i++)*(p+i)=i+1;//你在这个地方赋值时可能不对。
while(1)
{
x++;
if(x>=a) x-=a;
if(*(p+x)!=0) j++;
if(j%3==0&&*(p+x)!=0)
{
printf("%d\t",*(p+x));
*(p+x)=0;
j=0;
k++;
if(k==a) break;
}
}
}
试一下吧。

69,382

社区成员

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

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