欢迎来到天天文库
浏览记录
ID:25610369
大小:211.00 KB
页数:15页
时间:2018-11-21
《数据结构算法设计.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、数据结构算法设计1、使用顺序表实现约瑟夫环问题:设编号为1,2,3,……,n的n(n>0)个人按顺时针方向围坐一圈,m为任意一个正整数。从第一个人开始顺时针方向自1起顺序报数,报到m时停止并且报m的人出列,再从他的下一个人开始重新从1报数,报到m时停止并且报m的人出列。如此下去,直到所有人全部出列为止。要求设计一个程序模拟此过程,对任意给定的m和n,求出出列编号序列。C++代码如下:#include#include#definek20intJosephus(intn,intm);voidmain(){intn,m;cout<<"*******
2、*******约瑟夫环问题**************"<>n>>m;if(n>30){cout<<"此数组空间不够,超出了最大限定长度!"<3、4、m==0){cout<<"此次操作不符和题目要求!"<5、i]=i;cout<<"输出出列编号序列:"<=2;i--){s=(s+m-1)%i;if(s==0)s=i;cout<6、tream.h>#includevoidmain(){intx=0;structNode{intcoef;intexp;Node*next;};Node*frist,*second,*s,*p,*q,*pre,*qre,*v;frist=newNode;frist->next=NULL;second=newNode;second->next=NULL;cout<<"请输入多项式A(X):"<>s->coef>>s->exp;if(7、x==1){frist->next=s;p=s;}else{p->next=s;p=s;}cout<<"是否输入完毕(y/n):"<>i;if(i=='y'){cout<<"请在后面的操作中输入B(X)!!"<next=NULL;x=0;}else{i='y';}}p=frist->next;while(p!=NULL){cout<coef<<""<exp<next;}cout<<"请输入多项式B(X):"<8、指数:"<>s->coef>>s->exp;if(x==1){second->next=s;q=s;}else{q->next=s;q=s;}cout<<"是否输入完毕(y/n):"<>e;if(e=='y'){e='n';q->next=NULL;}else{e='y';}}q=second->next;while(q!=NULL){cout<coef<<""<exp<next;}pre=frist;p=pre->next;qre=second;q=qre->next;cout<<"多项9、式的和为:"<expexp){pre=p;p=p->next;}elseif(p->exp>q->exp){v=q->next;pre->next=q;q->next=p;q=v;cout<next->coef;}else{p->coef=p->coef+q->coef;if(p->coef==0){pre->next=p->next;deletep;p=pre->next;}
3、
4、m==0){cout<<"此次操作不符和题目要求!"<5、i]=i;cout<<"输出出列编号序列:"<=2;i--){s=(s+m-1)%i;if(s==0)s=i;cout<6、tream.h>#includevoidmain(){intx=0;structNode{intcoef;intexp;Node*next;};Node*frist,*second,*s,*p,*q,*pre,*qre,*v;frist=newNode;frist->next=NULL;second=newNode;second->next=NULL;cout<<"请输入多项式A(X):"<>s->coef>>s->exp;if(7、x==1){frist->next=s;p=s;}else{p->next=s;p=s;}cout<<"是否输入完毕(y/n):"<>i;if(i=='y'){cout<<"请在后面的操作中输入B(X)!!"<next=NULL;x=0;}else{i='y';}}p=frist->next;while(p!=NULL){cout<coef<<""<exp<next;}cout<<"请输入多项式B(X):"<8、指数:"<>s->coef>>s->exp;if(x==1){second->next=s;q=s;}else{q->next=s;q=s;}cout<<"是否输入完毕(y/n):"<>e;if(e=='y'){e='n';q->next=NULL;}else{e='y';}}q=second->next;while(q!=NULL){cout<coef<<""<exp<next;}pre=frist;p=pre->next;qre=second;q=qre->next;cout<<"多项9、式的和为:"<expexp){pre=p;p=p->next;}elseif(p->exp>q->exp){v=q->next;pre->next=q;q->next=p;q=v;cout<next->coef;}else{p->coef=p->coef+q->coef;if(p->coef==0){pre->next=p->next;deletep;p=pre->next;}
5、i]=i;cout<<"输出出列编号序列:"<=2;i--){s=(s+m-1)%i;if(s==0)s=i;cout<6、tream.h>#includevoidmain(){intx=0;structNode{intcoef;intexp;Node*next;};Node*frist,*second,*s,*p,*q,*pre,*qre,*v;frist=newNode;frist->next=NULL;second=newNode;second->next=NULL;cout<<"请输入多项式A(X):"<>s->coef>>s->exp;if(7、x==1){frist->next=s;p=s;}else{p->next=s;p=s;}cout<<"是否输入完毕(y/n):"<>i;if(i=='y'){cout<<"请在后面的操作中输入B(X)!!"<next=NULL;x=0;}else{i='y';}}p=frist->next;while(p!=NULL){cout<coef<<""<exp<next;}cout<<"请输入多项式B(X):"<8、指数:"<>s->coef>>s->exp;if(x==1){second->next=s;q=s;}else{q->next=s;q=s;}cout<<"是否输入完毕(y/n):"<>e;if(e=='y'){e='n';q->next=NULL;}else{e='y';}}q=second->next;while(q!=NULL){cout<coef<<""<exp<next;}pre=frist;p=pre->next;qre=second;q=qre->next;cout<<"多项9、式的和为:"<expexp){pre=p;p=p->next;}elseif(p->exp>q->exp){v=q->next;pre->next=q;q->next=p;q=v;cout<next->coef;}else{p->coef=p->coef+q->coef;if(p->coef==0){pre->next=p->next;deletep;p=pre->next;}
6、tream.h>#includevoidmain(){intx=0;structNode{intcoef;intexp;Node*next;};Node*frist,*second,*s,*p,*q,*pre,*qre,*v;frist=newNode;frist->next=NULL;second=newNode;second->next=NULL;cout<<"请输入多项式A(X):"<>s->coef>>s->exp;if(
7、x==1){frist->next=s;p=s;}else{p->next=s;p=s;}cout<<"是否输入完毕(y/n):"<>i;if(i=='y'){cout<<"请在后面的操作中输入B(X)!!"<next=NULL;x=0;}else{i='y';}}p=frist->next;while(p!=NULL){cout<coef<<""<exp<next;}cout<<"请输入多项式B(X):"<8、指数:"<>s->coef>>s->exp;if(x==1){second->next=s;q=s;}else{q->next=s;q=s;}cout<<"是否输入完毕(y/n):"<>e;if(e=='y'){e='n';q->next=NULL;}else{e='y';}}q=second->next;while(q!=NULL){cout<coef<<""<exp<next;}pre=frist;p=pre->next;qre=second;q=qre->next;cout<<"多项9、式的和为:"<expexp){pre=p;p=p->next;}elseif(p->exp>q->exp){v=q->next;pre->next=q;q->next=p;q=v;cout<next->coef;}else{p->coef=p->coef+q->coef;if(p->coef==0){pre->next=p->next;deletep;p=pre->next;}
8、指数:"<>s->coef>>s->exp;if(x==1){second->next=s;q=s;}else{q->next=s;q=s;}cout<<"是否输入完毕(y/n):"<>e;if(e=='y'){e='n';q->next=NULL;}else{e='y';}}q=second->next;while(q!=NULL){cout<coef<<""<exp<next;}pre=frist;p=pre->next;qre=second;q=qre->next;cout<<"多项
9、式的和为:"<expexp){pre=p;p=p->next;}elseif(p->exp>q->exp){v=q->next;pre->next=q;q->next=p;q=v;cout<next->coef;}else{p->coef=p->coef+q->coef;if(p->coef==0){pre->next=p->next;deletep;p=pre->next;}
此文档下载收益归作者所有