圣安地列斯怎么改速度:用VC++写的约瑟夫环~大家看看是什么问题呀

来源:百度文库 编辑:高考问答 时间:2024/04/30 14:18:53
小女子学的不好 好请大家帮我改改~是逻辑错误

#include<iostream.h>
struct LNode{
int data,code;
LNode *next;
};
void Create(LNode *head) //初始化单向循环链表
{
LNode *p;
int i,n;
cout<<"输入各人密码:"<<endl;
head->data=1;
cin>>head->code ;
p=head;
for(i=2;i<=n;i++){
LNode *s=new LNode;
s->data=i;
cin>>s->code;
p->next=s;
p=p->next;
}
p->next=head;
}
int Put(LNode *q) //处理出列顺序
{
int i,n,m;
LNode *p;
while(n!=1){
for(i=1;i<m;i++)
p=p->next;
q=p->next;
p->next=q->next;
m=q->code;
return q->data;
delete q;
n--;
}
return p->data;
}
void main()
{
LNode *head,*q;
int m,n,i;
cout<<"输入初始报数上限值m: "<<endl;
cin>>m;
cout<<"输入人数n: "<<endl;
cin>>n;
Create(head);
cout<<"出列顺序为: "<<endl;
Put(q);
}
这个程序可以得出那个结果 我觉得两个没什么区别呀~为什么结果不一样呢
#include<iostream.h>

struct LNode{
int data,code;
LNode *next;
};

void main()
{
LNode *p,*head,*q;
int m,n,i;
cout<<"输入初始报数上限值m: "<<endl;
cin>>m;
cout<<"输入人数n: "<<endl;
cin>>n;
cout<<"输入各人密码:"<<endl;
head=new LNode;
head->data=1;
cin>>head->code ;
p=head;

//初始化单向循环链表
for(i=2;i<=n;i++)
{
LNode *s=new LNode;
s->data=i;
cin>>s->code;
p->next=s;
p=p->next;
}
p->next=head;
cout<<"出列顺序为: "<<endl;

//处理出列顺序
while(n!=1)
{
for(i=1;i<m;i++)
{
p=p->next;
}
q=p->next;
p->next=q->next;
m=q->code;
cout<<q->data<<' ';
delete q;
n--;
}
cout<<p->data<<endl;
}

仔细想了一下,出列顺序的确应该是6 1 4 7 2 3 5。
细细分析了一遍程序发现你的put函数有逻辑错误。修改如下:

///////////////////////////////////////////////
#include<iostream.h>

struct LNode{
int data,code;
LNode *next;
};

int m,n;

void Create(LNode *head) //初始化单向循环链表
{
LNode *p;
int i;
cout<<"输入各人密码:"<<endl;
head->data=1;
cin>>head->code ;
p=head;
for(i=2;i<=n;i++)
{
LNode *s=new LNode;
s->data=i;
cin>>s->code;
p->next=s;
p=s;
}
p->next=head;
}

int Put(LNode *q) //处理出列顺序
{
int i;
LNode *p;
p=q;
while(n!=1)
{
for(i=2;i<=m;i++)
{
q=p;
p=p->next;
}
q->next=p->next;
m=p->code;
cout<<p->data<<endl;
delete p;
p=q->next;
n--;
i=2;
}
return p->data;
}

void main()
{
LNode *head,*q;
head=new LNode;
q=head;
cout<<"输入初始报数上限值m: "<<endl;
cin>>m;
cout<<"输入人数n: "<<endl;
cin>>n;
Create(head);
cout<<"出列顺序为: "<<endl;
cout<<"最后留下的是"<<Put(q)<<"号"<<endl;
}

路过,没出息,看不懂。