约瑟夫环问题!(有问题改不下去了,求帮忙!)

约瑟夫环问题!(有问题改不下去了,求帮忙!),第1张

Status Joseph(CNode *clist, int m, int n, int k)

{ int iCNode *p,*q

if(m>n) return ERROR

if (!Create_clist(clist,n) ) return ERROR

p=joseph

for(i=1i<mi++) p=p->next

while(p)

{ for (i=1i<k-1i++) 改为{ for (i=1i<ki++) 因为要删除的是第k个,当i=1时,p指向开 p=p->next 始数的第一个元素,所以要找到要删除的元素的消判此前一个元素

q=p->next 此冲橘时q为要删拿迅除的元素

printf("%d ",q->data)

if (p->next==p)

p=NULL

else { p->next=q->next

p=p->next 这一句要注释掉

free(q)}

} /*while */ clist=NULL

} /* end */

#include <iostream>

#include <vector>

using namespace std

/冲和/该程序调用容器vector,但是main函数的内容无需改动,面向对猛陪象

struct Player

{

int Number//游戏者的编号

int Secret //游戏者的密码

Player&operator=(Player&T)

{

Number = T.Number

Secret = T.Secret

}

}

class LinkList

{

private:

vector<Player>::iterator ptr //不同之处在于ptr是向量的指针

vector<Player>Circle

int delPlayer()

public:

LinkList() { ptr=Circle.begin() }

LinkList(int n) //创建一个长度为n的链表

void playing(int&,int&)

void output_test()

}

LinkList::LinkList(int n)

{

int Secret

Player T

for(int i=1i<=ni++)

{

cout<<"enter the password for player-" <<i <<": "

cin>>Secret

T.Number=i

T.Secret=Secret

Circle.push_back(T)

}

ptr=Circle.begin() //指向编号为1的游戏者

}

int LinkList::delPlayer()

{

int Secret

if (Circle.empty()) return -1

Secret=ptr->Secret

cout <<ptr->Number<<endl

ptr=Circle.erase(ptr)

ptr++

if (ptr==Circle.end()) ptr=Circle.begin()

return Secret

}

void LinkList::playing(int &initialCode,int&n)

{

int m, k = 1

m = initialCode % n? initialCode % n: initialCode

while (n>0)

{

if (k == m || n == 1)

{

m = delPlayer()

n--

if( n==0 ) break

m = m%n? m%n : m

k = 0 //重新做标记从下一个人开始

}

else

{

ptr++

if (ptr==Circle.end()) ptr=Circle.begin()

}

k++

}

}

void LinkList::output_test()

{

while (ptr!=Circle.end())

{

cout<<"("<<ptr->散知盯Number<<","<<ptr->Secret<<")\t"

ptr++

}

cout <<endl

ptr=Circle.begin()

}

//end

int main()

{

int it,n

cout<<"Enter the number of players and initial password:"

cin >>n >>it

LinkList aJosephCircle(n)

aJosephCircle.output_test()

aJosephCircle.playing(it,n)

system("pause")

return 0

}

//下面的是多项式的源代码

//因为比较匆忙,没有排序,所以输入需降序

#include <iostream>

using namespace std

struct term

{

float coef

int expn

term* next

}

class Polynomial

{

private:

term* L

term head

term* MakeNode(float c,int e)

bool LocateTerm(int e)

int Compare(int e)

public:

Polynomial()

term* GetL()

void InBefore(term* local,term* t)

void InsertIf(int i,term* t)

void DelNode(term* )

void Free(term* )

void Append(term* )

void CreatePoly(int m)

void PrintPoly()

int PolyLength()

void AddPoly(Polynomial&p)

void SubPoly(Polynomial&p)

}

Polynomial::Polynomial()

{

L=&head

head.next=NULL

}

term* Polynomial::MakeNode(float c,int e)

{

term* t=new term

t->coef=c

t->expn=e

t->next=NULL

return t

}

int Polynomial::Compare(int e)

{

term* t1

int count=0

t1=L

for(t1=t1->next)

{

count++

if(!t1) break

if(e>(t1->expn)) break

}

return count

}

bool Polynomial::LocateTerm(int e)

{

term* temp

for(temp=L->nexttemp!=NULLtemp=temp->next)

if(temp->expn==e) return true

return false

}

term* Polynomial::GetL()

{

return L

}

void Polynomial::InBefore(term* local,term* t)

{

term* temp

for(temp=Ltemp->next!=local)

temp=temp->next

temp->next=t

t->next=local

}

void Polynomial::InsertIf(int i,term* t)

{

term* temp=L

for(int j=0j<ij++)

if(temp->next) temp=temp->next

t->next=temp->next

temp->next=t

}

void Polynomial::DelNode(term* t)

{

term* temp

for(temp=Ltemp->next!=t)

temp=temp->next

temp->next=t->next

}

void Polynomial::Free(term* t)

{

delete t

}

void Polynomial::Append(term* t)

{

term* temp

for(temp=Ltemp->next!=NULL)

temp=temp->next

temp->next=t

}

void Polynomial::CreatePoly(int m)

{

float c

int e

cout<<"输入一元多项式各个项(系数、指数):"<<endl

for(int i=1i<=mi++)

{

cout<<"请输入第"<<i<<"项系数"

cin>>c

cout<<"请输入第"<<i<<"项指数"

cin>>e

if(!LocateTerm(e)) InsertIf(Compare(e),MakeNode(c,e))

}

}

void Polynomial::PrintPoly()

{

term* temp

for(temp=L->nexttemp!=NULLtemp=temp->next)

{

cout<<temp->coef<<"X"<<temp->expn<<" "

}

cout<<endl

}

void Polynomial::AddPoly(Polynomial&p)

{

term* Lb=p.GetL()

term* pa=L->next

term* pb=Lb->next

term* temp

int a,b

while(pa&&pb)

{

a=pa->expn

b=pb->expn

if(a<b) pa=pa->next

if(a=b)

{

float sum=pa->coef+pb->coef

if(sum!=0.0) {pa->coef=sum pa=pa->next}

else {temp=papa=temp->nextDelNode(temp)Free(temp)}

temp=pb pb=temp->next p.DelNode(temp)p.Free(temp)

}

if(a>b)

{

temp=pb

pb=temp->next

p.DelNode(temp)

InBefore(pa,temp)

}

}

if(Lb->next!=NULL) Append(pb)

p.Free(Lb)

}

void Polynomial::SubPoly(Polynomial&p)

{

term* Lb=p.GetL()

term* pa=L->next

term* pb=Lb->next

term* temp

int a,b

while(pa&&pb)

{

a=pa->expn

b=pb->expn

if(a<b) pa=pa->next

if(a=b)

{

float sum=pa->coef-pb->coef

if(sum!=0.0) {pa->coef=sum pa=pa->next}

else {temp=papa=temp->nextDelNode(temp)Free(temp)}

temp=pb pb=temp->next p.DelNode(temp)p.Free(temp)

}

if(a>b)

{

temp=pb

pb=temp->next

p.DelNode(temp)

InBefore(pa,temp)

temp->coef=-(temp->coef)

}

}

if(Lb->next!=NULL) Append(pb)

p.Free(Lb)

}

int main()

{

Polynomial pa,pb

int m,n

cout<<"请输入多项式pa、pb的长度:"

cin>>m>>n

pa.CreatePoly(m)

pa.PrintPoly()

pb.CreatePoly(n)

pb.PrintPoly()

pa.AddPoly(pb)

pa.PrintPoly()

system("pause")

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8229387.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存