用C语言 写了一个约瑟夫环 可是报错了 找了很久 没找出错误 求高手指出 源代码 以及执行结果如下。 40
#include<stdio.h>#include<stdlib.h>structcircularList{intnum;intdata;structcircularLi...
#include<stdio.h>#include<stdlib.h>struct circularList{ int num; int data; struct circularList *next;};struct circularList *creatList(){ struct circularList *head,*rear,*s; int a,b=0; head=(struct circularList *)malloc(sizeof(struct circularList)); head->data=1; //问题1:如何让head->data和num不显示在约瑟夫环之内?;
head->num=0; rear=head; printf("please input password(以0结束):"); scanf("%d",&a); while(a){ s=(struct circularList *)malloc(sizeof(struct circularList)); s->data=a; s->num=b+1; b=b+1; rear->next=s; rear=s; scanf("%d",&a); } rear->next=head; return head; } void josephRing(struct circularList *p){ struct circularList *t; int i,m,a,b; printf("please input m value(整数):"); scanf("%d",&m); for(i=0;i<m;i++){ p=p->next; } t=p->next; a=p->data; b=p->num; printf("%d ",b); free(p); p=t; while(p!=NULL){ for(i=0;i<a;i++){ p=p->next; } t=p->next; a=p->data; b=p->num; printf("%d ",b); free(p); p=t; }}void main(){ struct circularList *A; A=creatList(); josephRing(A); } //问题二 :怎么报错了??怎么回事 ??帮忙并修改一下 谢谢了( ^_^ ) 展开
head->num=0; rear=head; printf("please input password(以0结束):"); scanf("%d",&a); while(a){ s=(struct circularList *)malloc(sizeof(struct circularList)); s->data=a; s->num=b+1; b=b+1; rear->next=s; rear=s; scanf("%d",&a); } rear->next=head; return head; } void josephRing(struct circularList *p){ struct circularList *t; int i,m,a,b; printf("please input m value(整数):"); scanf("%d",&m); for(i=0;i<m;i++){ p=p->next; } t=p->next; a=p->data; b=p->num; printf("%d ",b); free(p); p=t; while(p!=NULL){ for(i=0;i<a;i++){ p=p->next; } t=p->next; a=p->data; b=p->num; printf("%d ",b); free(p); p=t; }}void main(){ struct circularList *A; A=creatList(); josephRing(A); } //问题二 :怎么报错了??怎么回事 ??帮忙并修改一下 谢谢了( ^_^ ) 展开
展开全部
以前写过这程序。
远远没有你写的这么长。
#include <stdio.h>
#define N 17 //人数
#define M 11 //出局人号码
void main()
{
int a[N], i, call_n = 0, out_n = 0;
for (i = 0; i < N; i++) a[i] = 0;
i = 0;
while (1) { //循环报数
if(a[i] == 0) { //如果健在
if (out_n == (N - 1)) break; //如果仅剩一人
call_n++; //报数
call_n %= M; //最大为M,到了M就从0开始
if(call_n == 0) {
a[i] = 1; //出局标记
out_n++;
printf("%d ", i + 1);//显示出局人号码
}
}
i++; i %= N; //循环转向下一人
}
printf("\n最后剩余者的编号是:%d\n", i + 1);
}
//------------------完
参考:
http://hi.baidu.com/do_sermon/item/e325a8d627fa39bd33db909b
http://hi.baidu.com/do_sermon/item/d65a203c1cd68b139cc65ea9
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询