未解决的问题
小弟愚昧 就是调试不出结果
还望高手指点
约瑟夫(Joseph)问题的一种描述是:编号为1,2,3,…,n的n个人按顺时针方向围坐一圈。每人持有一个密码(正整数)。一开始人选一个正整数作为报数上限值m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报数。报m的认出列,将他的密码作为新的m值,从他在顺时针方向上的下一个人开始从新报数,如此下去,直至所有人全部出列为止。试设计一个程序求出出列顺序。
#include <malloc.h>
typedef struct lnode
{
int number;
int cipher;
struct lnode *next;
};
struct lnode *creatlink(int n)
{
int a=0;
struct lnode *head1,*p1,*q1;
p1=q1=(struct lnode *)malloc(sizeof(struct lnode));
printf("input the %d cipher:",a+1);
scanf("%d",&p1->cipher);
a=a+1;
p1->number=a;
while(a<n)
{
if(a==1) head1=p1;
else q1->next=p1;
q1=p1;
p1=(struct lnode *)malloc(sizeof(struct lnode));
printf("input the %d cipher:",a+1);
scanf("%d",&p1->cipher);
p1->number=a+1;
a++;
}
q1->next=head1;
return(head1);
}
main()
{
int i,j,n,m;
struct lnode *head2,*p2,*q2;
printf("input the number:");
scanf("%d",&n);
head2=p2=q2=creatlink(n);
printf("input the last read number:");
scanf("%d",&m);
for(i=n;i>0;i--)
{
j=1;
while(j<m)
{
q2=p2;
p2=p2->next;
j++;
}
q2=p2->next;
m=p2->cipher;
free(p2);
p2=q2;
}
}
问题点数:100、回复次数:4Top
1 楼zhang_jiang(Solar)回复于 2005-04-24 19:26:34 得分 20
用// !!!表出改动处
#include <malloc.h>
struct lnode // !!!
{
int number;
int cipher;
struct lnode *next;
};
struct lnode *creatlink(int n)
{
int a=0;
struct lnode *head1,*p1,*q1;
p1=q1=(struct lnode *)malloc(sizeof(struct lnode));
printf("input the %d cipher:",a+1);
scanf("%d",&p1->cipher);
a=a+1;
p1->number=a;
while(a<n)
{
if(a==1) head1=p1;
else q1->next=p1;
q1=p1;
p1=(struct lnode *)malloc(sizeof(struct lnode));
printf("input the %d cipher:",a+1);
scanf("%d",&p1->cipher);
p1->number=a+1;
a++;
}
q1->next=head1;
return(head1);
}
main()
{
int i,j,n,m;
struct lnode *head2,*p2,*q2;
printf("input the number:");
scanf("%d",&n);
head2=p2=q2=creatlink(n);
printf("input the last read number:");
scanf("%d",&m);
for(i=n;i>0;i--)
{
j=1;
while(j<m)
{
q2=p2;
p2=p2->next;
j++;
}
q2->next=p2->next; // !!!
m=p2->cipher;
free(p2);
p2=q2->next; // !!!
}
}Top
2 楼zhang_jiang(Solar)回复于 2005-04-24 19:44:01 得分 0
又改了一下, 如下:
#include <malloc.h>
struct lnode // !!!
{
int cipher;
struct lnode *next;
};
struct lnode * createlink(int n, int m)
{
int i, tmp, flag;
struct lnode *head1,*p1,*q1;
for(i=0; i<n; i++)
{
printf("Input cipher for node %d:", i+1);
scanf("%d", &tmp);
flag =0;
while(!flag)
{
if(tmp > m)
{
printf("cipher greater than m, input again:");
scanf("%d", &tmp);
}
else
flag = 1;
}
p1=(struct lnode *)malloc(sizeof(struct lnode));
p1->cipher = tmp;
if(!i)
{
head1=p1;
q1=p1;
}
else
{
q1->next=p1;
q1=p1;
}
}
q1 ->next =head1; //形成循环
return head1;
}
int main()
{
int i,j,n,m;
struct lnode *head2,*p2,*q2;
printf("input the number:");
scanf("%d", &n);
printf("input the max cipher number:");
scanf("%d",&m);
q2 =head2 =createlink(n, m);
p2=q2->next;
for(i=n;i>0;i--)
{
j=1;
while(j<m)
{
q2=p2;
p2=p2->next;
j++;
}
q2->next=p2->next; // !!!
m=p2->cipher;
printf("%d\n", m);
free(p2);
p2=q2->next; // !!!
}
return 1;
}Top
3 楼zengwujun(月之海 为linux入门奋斗100天)回复于 2005-04-24 19:54:27 得分 70
根据你的思路改写的
#include <malloc.h>
#include <stdio.h>
struct lnode
{
int number;
int cipher;
struct lnode *next;
};
struct lnode *creatlink(int n)
{
int a=0;
struct lnode *head,*tail,*p;
while(a<n)
{
p=(struct lnode *)malloc(sizeof(struct lnode));
p->number=a+1;
p->next=0;
printf("input the %d cipher:",a+1);
scanf("%d",&p->cipher);
a++;
if(a==1) {head=p;tail=head;}
else {tail->next=p;tail=p;}
}
tail->next=head;
return(head);
}
void main()
{
int i,j,n,m;
struct lnode *head,*pre,*p;
printf("input the number:");
scanf("%d",&n);
head=p=creatlink(n);
printf("input the last read number:");
scanf("%d",&m);
for(i=n;i>0;i--)
{
j=1;
//查找要删除的节点
while(j<m)
{
pre=p;
p=p->next;
j++;
}
m=p->cipher;//密码改
printf("%4d",p->number);
pre->next=p->next;
free(p);
p=pre->next;
}
printf("\n");
}Top
4 楼llmsn("若虚"即"虚怀若谷"!!!)回复于 2005-04-24 20:54:48 得分 10
帖一个别人写的.
#include "stdio.h"
#include "malloc.h"
#define M 50
typedef struct node{
int data;
struct node *link;}JD;
void Josephu(int n,int s)
{
JD *h,*p,*q,*r;
int i,j,l[M],k=1;
h=(JD*)malloc(sizeof(JD));
h->data=1;
r=h;
for(i=2;i<=n;i++)
{ p=(JD*)malloc(sizeof(JD));
p->data=i;
r->link=p;
r=p;
}
r->link=h;
printf("The list is: ");
p=h;
for(i=1;i<=n;i++)
{ printf("%d",p->data);
p=p->link;
}
printf("\n");
q=NULL;
p=h;
j=1;
do
{
while(j!=s)
{ j++;
q=p;
p=p->link;
}
l[k]=p->data;
q->link=p->link;
free(p);
p=q->link;
j=1;
k++;
}while(p->link!=p);
l[k]=p->data;
printf("The sort is: ");
for(i=1;i<=k;i++)
printf("%d",l[i]);
printf("\n");
}
main()
{
int n,s;
printf("input n and s:");
scanf("%d,%d",&n,&s);
Josephu(n,s);
}
Top




