C语言数据结构josephus问题解决方案 帮我改正下
#include<stdio.h>#include<stdlib.h>structstudent{intnum;structstudent*next;};intlengt...
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
struct student * next;
};
int length=0;
struct student * create()
{
struct student * p1,* p2,* head;
head=NULL;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("请输入序号:");
scanf("%d",&p1->num);
while(p1->num!=0)
{
length++;
if(p1->num==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
printf("请输入序号:");
scanf("%d",&p1->num);
};
if(length!=0)
p2->next=head;
else
printf("输入完毕!\n\n");
return head;
}
struct student * find(struct student * head,int s)
{
struct student * p=head;
int i=s;
for(;i>1;i--)
if(p->next!=NULL)p=p->next;
printf("%d ",p->num);
return p;
}
struct student * del(struct student * head,int m)
{
struct student * p1,* p2;
p1=p2=head;
p1=p1->next;
for(;m>1;m--)
{
p2=p1;
p1=p1->next;
}
p2=p1->next;
length--;
printf("%d ",p1->num);
return p1->next;
}
void main()
{
struct student * head;
int n,s,m;
head=create();
printf("请输入开始的序号:");
scanf("%d",&s);
head=find(head,s);
printf("请输入每次轮过的人数:");
scanf("%d",&m);
printf("答案为:");
for(n=length;n>0;n--)
head=del(head,m);
printf("\n");
}
各位大手子帮我看下我是哪里错了.
顺序输入能解出,乱序输入会出现错误答案. 展开
#include<stdlib.h>
struct student
{
int num;
struct student * next;
};
int length=0;
struct student * create()
{
struct student * p1,* p2,* head;
head=NULL;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("请输入序号:");
scanf("%d",&p1->num);
while(p1->num!=0)
{
length++;
if(p1->num==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
printf("请输入序号:");
scanf("%d",&p1->num);
};
if(length!=0)
p2->next=head;
else
printf("输入完毕!\n\n");
return head;
}
struct student * find(struct student * head,int s)
{
struct student * p=head;
int i=s;
for(;i>1;i--)
if(p->next!=NULL)p=p->next;
printf("%d ",p->num);
return p;
}
struct student * del(struct student * head,int m)
{
struct student * p1,* p2;
p1=p2=head;
p1=p1->next;
for(;m>1;m--)
{
p2=p1;
p1=p1->next;
}
p2=p1->next;
length--;
printf("%d ",p1->num);
return p1->next;
}
void main()
{
struct student * head;
int n,s,m;
head=create();
printf("请输入开始的序号:");
scanf("%d",&s);
head=find(head,s);
printf("请输入每次轮过的人数:");
scanf("%d",&m);
printf("答案为:");
for(n=length;n>0;n--)
head=del(head,m);
printf("\n");
}
各位大手子帮我看下我是哪里错了.
顺序输入能解出,乱序输入会出现错误答案. 展开
展开全部
给你改了一个,功能划分上有点变化,不知是否符合你的要求:
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
struct student * next;
};
int length=0;
void display(struct student *h)
{
int i;
struct student *p=h;
if(length==0){
printf("链表为空。\n");
return;
}
for(i=0; i<length; i++)
{
printf("%d ", p->num);
p=p->next;
}
printf("\n");
}
struct student * create()
{
struct student *p1, *p2, *head=0;
int n;
while(1)
{
printf("请输入序号:");
scanf("%d",&n);
if(n==0) break;
p1=(struct student *)malloc(sizeof(struct student));
if(!p1) break;
length++;
p1->num=n;
if(length==1)
head=p1, p2=p1;
else
p2->next=p1, p2=p1;
}//while(1)
if(length!=0)
p2->next=head;
printf("输入完毕!\n\n");
return head;
}
//从序号为begin的人开始报数,报到step的人出列
void outring(struct student *h, int begin, int step)
{
struct student *p=h, *q;
int c, i;
while(p->num!=begin) p=p->next;
for(i=0; i<length; i++)
{
for( c=1,q=p; c<step; q=p, p=p->next, c++); //报数
q->next=p->next;
q=p;
printf("%d ", q->num);
p=p->next;
free((struct student*)q);
}
printf("\n");
}
void main()
{
struct student * head;
int s,m;
head=create();
display(head);
printf("请输入开始的序号:");
scanf("%d",&s);
printf("请输入每次轮过的人数:");
scanf("%d",&m);
printf("答案为:");
outring(head,s,m);
}
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
struct student * next;
};
int length=0;
void display(struct student *h)
{
int i;
struct student *p=h;
if(length==0){
printf("链表为空。\n");
return;
}
for(i=0; i<length; i++)
{
printf("%d ", p->num);
p=p->next;
}
printf("\n");
}
struct student * create()
{
struct student *p1, *p2, *head=0;
int n;
while(1)
{
printf("请输入序号:");
scanf("%d",&n);
if(n==0) break;
p1=(struct student *)malloc(sizeof(struct student));
if(!p1) break;
length++;
p1->num=n;
if(length==1)
head=p1, p2=p1;
else
p2->next=p1, p2=p1;
}//while(1)
if(length!=0)
p2->next=head;
printf("输入完毕!\n\n");
return head;
}
//从序号为begin的人开始报数,报到step的人出列
void outring(struct student *h, int begin, int step)
{
struct student *p=h, *q;
int c, i;
while(p->num!=begin) p=p->next;
for(i=0; i<length; i++)
{
for( c=1,q=p; c<step; q=p, p=p->next, c++); //报数
q->next=p->next;
q=p;
printf("%d ", q->num);
p=p->next;
free((struct student*)q);
}
printf("\n");
}
void main()
{
struct student * head;
int s,m;
head=create();
display(head);
printf("请输入开始的序号:");
scanf("%d",&s);
printf("请输入每次轮过的人数:");
scanf("%d",&m);
printf("答案为:");
outring(head,s,m);
}
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询