ImageVerifierCode 换一换
格式:DOCX , 页数:78 ,大小:34.47KB ,
资源ID:11197287      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11197287.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++面向对象 学生作品.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++面向对象 学生作品.docx

1、C+面向对象 学生作品学生作品集目 录作品1:0-1背包动态规划 1作品2:n皇后问题 2作品3:病人挂号系统 3作品4:挂号程序 5作品5:递归的二分查找 7作品6:动态规划最短路径 8作品7:非递归的二分查找 10作品8:非递归实现的迷宫问题 11作品9:汉诺塔 16作品10:皇后递归 16作品11:皇后非递归 18作品12:活动选择问题 20作品13:积分程序 21作品14:矩阵链乘算法 22作品15:矩阵链乘动态规划算法 24作品16:快速排序 25作品17:迷宫的非递归算法 26作品18:售后服务系统 28作品19:贪婪法背包问题 32作品20:贪心背包算法1 33作品21:贪心背包

2、算法2 34作品22:贪心链表背包 35作品23:学生成绩查询 38作品24:医院挂号程序 39作品25:用递归实现的皇后问题 41作品26:用类的继承来实现的各种方法的积分 43作品27:运算符重载 45作品1:0-1背包动态规划#include#define n 5#define W 17main() int w,i;int cn+1W+1; /背包容量为w时有i件可选物体所导致的最优解的总价值 int w1n+1=0,3,4,7,8,9; /物品重量 int vn+1=0,4,5,10,11,13; /物品价值 int xn+1=0; /初始的物品选择 /物品个数或背包容量为0时总价值都

3、为0 for(w=0;w=W;w+) c0w=0; for(i=1;i=n;i+) ci0=0; /自底向上推 for(i=1;i=n;i+) /按物品初始化时顺序依次尝试加入背包,初始化时顺序可变 for(w=1;w=W;w+) if(w1ici-1w) ciw=vi+ci-1w-w1i; else ciw=ci-1w; / 用不着第I件,前I-1件已满足条件,可能还有剩余 /ciw=ci-1w中不仅有i-1到i的传递,也包含i-2经i-1到i的传递 else ciw=ci-1w; printf(%dn,cnW); w=W; for(i=n;i=1;i-) /根据自底向上推时判断的过程回推

4、if(ciw=ci-1w) xi=0; else xi=1; w=w-w1i; for(i=1;i=5;i+) printf(%dn,xi); getch();作品2:n皇后问题#include stdio.h#define M 10int xM;bool place(int k,int j) int i; for(i=1;ik;i+) if(xi=j)|(xk-i=j+i)|(xk-i=j-i) return false; return true;bool queen(int k,int n) int j; for(j=1;j=n;j+) if(place(k,j)=true) xk=j;

5、if(k=n) return true; else if(queen(k+1,n)=true) return true; else xk=0; continue; return false; void main() int n,k; printf(qing shu ru qi zi d ge shu:); scanf(%d,&n); for(k=1;k=n;k+) queen(k,n); for(k=1;k=n;k+)printf(第%d行排在第%d列n,k,xk); 作品3:病人挂号系统#includeiostream.hstruct patient int num;char name10;

6、char sex6;int age;patient* next;void main()patient *head=NULL,*tail=NULL; patient *p,*q,*temp; char choice,choice2; I1: cout挂号请输入A;endl 插入请输入I;endl 删除请输入D;endl 就诊请输入B;endl 查看状态请输入L;endl 保存关机请输入S.endl choice; if (choice=A) /添加I2: p=new patient; coutp-nump-namep-sexp-age; p-next=NULL; if(head=NULL) he

7、ad=p;tail=p; else tail-next=p;tail=p; cout添加成功!endl choice2; if (choice2=J) break; else if (choice2=C) goto I2; else if(choice=I) /插入 temp=new patient; couttemp-numtemp-nametemp-sextemp-age; int i_num; couti_num; p=head; while(1) if(p-num!=i_num) q=p;p=p-next; else break; if (p=NULL) coutnext=temp;

8、temp-next=q-next; tail=temp; cout插入成功,请执行其他操作.endl; goto I1; else if(choice=D) /删除 coutd_num; p=head; while(1) if(p-num!=d_num) q=p;p=p-next; else break; if (p=NULL) coutnext=p-next; delete p; cout删除成功,请执行其他操作.endl; goto I1; else if(choice=B) /就诊 if (head=tail=NULL) couterror; else coutnumname; p=he

9、ad;head=head-next; delete p; cout请下一位病人做好准备.; else if(choice=L) /查看状态 p=head; cout当前列表状态如下:endl; while (p!=NULL) coutnum name sex agenext; cout请继续执行其他操作:endl; goto I1; else if(choice=S) cout保存关机; break; 作品4:挂号程序#includestruct patientint num,age,sex; char name20; patient *next;void main()patient *hea

10、d=NULL,*tail=NULL,*p,*q;char ch;int num,n,age,sex;cout挂号请按A endl;cout治病请按Bendl;cout查看请按Cendl; cout跳出请按D endl;cout删除号码请按Eendl;cout插入请按Fch;if (ch=A)p=new patient;cout请输入病人的信息p-nump-namep-agep-sex; /挂号p-next=NULL;if(head=tail=NULL)head=p; tail=p;elsetail-next=p;tail=p;cout完成,可以进行其他操作;else if (ch=B)if(h

11、ead=tail=NULL)couterrorendl;elsecoutnumnameagesex; /治病p=head;head=head-next;delete p;cout完成,可以进行其他操作;else if (ch=C)p=head;while (p!=NULL) /查看coutnumnameagesexnext;cout完成,可以进行其他操作;if (ch=E)coutd; /删除p=head;while(1)if(p-num!=d)q=p; p=p-next;if(p=NULL)coutnext=p-next;delete p;if(ch=F)p=new patient;cout

12、d;p=head;while(1)if(p-num)!=(p-num)q=p; p=p-next;else q-next=p; p-next=q;for(num=q-num;numn;num+) /插入num=num+1;coutp-nump-namep-agesex;else if(ch=D)break; /跳出作品5:递归的二分查找#includesearch(int *p,int v,int low,int high) int mid; while(lowpmid) low=mid+1; else high=mid-1; return 0; main() int i,j,k,temp,a

13、11,v,t; printf(请输入一组数字:n); for (i=1; i11;i+) scanf(%d,&ai); for (i=1; i11-1;i+) k=i; for (j=i+1;j11;j+) if ( ajak) k=j; temp=ak; ak=ai; ai=temp; printf(请输入v值:n); scanf(%d,&v); t=search(a,v,1,10); printf(数组排好序后v值在数组中的位置是:%dn,t);作品6:动态规划最短路径/ 用动态规划求两点之间的最短路径#include stdafx.h#include iostream.h#define

14、M 2 /分成2段#define K 3 /各段中得最多节点数#define N 4int graphNN; /邻接矩阵 int segM+1K; /分段信息int costN+1; /节点到终点的最短距离int dM; /最优路径下的下一个节点 int sM+1; /每一段的节点数void call(int h,int &p)/计算 int i,j,T,tag=0;/赋值cout最后一段各结点到终点的距离!endl;for(i=0;isM-1;i+) costsegM-1i=graphsegM-1iN-1; coutcostsegM-1i ;coutendl;/*for(i=0;ish;i+

15、)for(j=0;jsh+1;j+)if(tag=0&graphseghisegh+1j)T=graphseghisegh+1j+costsegh+1j;tag=1;/8888888888888888888888888888if(tag=1)break;if(tag=0) cout无路可走!endl; return;/888888888888888888888888888/*for(;ish;i+)for(;jgraphseghisegh+1j +costsegh+1j)costseghi=graphseghisegh+1j;p=seghi;/*8void main() int i,j,sum

16、=0; costN=0; cout请输入邻接矩阵!endl; for(i=0;iN-1;i+) for(j=0;jgraphij; cout请输入每段的结点数!endl; for(i=0;isi; cout每段的结点数!endl; for(i=0;iM;i+) coutsi ; coutendl; seg00=0; for(i=1;i=M;i+) sum+=si-1; for(j=0;jsi;j+) segij=sum+j; cout每个结点的关键字为!endl;for(i=0;i=M;i+) for(j=0;jsi;j+) coutsegij ;cout=0;i-)call(i,di);co

17、ut最优路径为!endl;for(i=0;iM;i+)coutdi;coutN-1endl;作品7:非递归的二分查找/输入十个数,然后用二分查找法从中查找一个数#includemain() int a11,i,j,temp,mid,high,low,b ; printf(Input 10 numbers:n); for(i=1;i11;i+) scanf(%d,&ai); /输入十个数 for(i=1;i11;i+) /选择排序这十个数 for(j=i+1;jaj) temp=ai; ai=aj; aj=temp ; for(j=1;j11;j+) printf(the sorted numb

18、ers:%dn,aj); printf( ); scanf(%d,&b); /输入要查找的数 low=1 ; high=10 ; while(lowamid) low=mid+1 ; else high=mid-1 ; printf(the number%d is not foundn,b); /未找到作品8:非递归实现的迷宫问题#include iostream.hconst int m=4,n=4; const int maxsize=m*n; /定义迷宫大小struct offint a,b; /定义方向const off move8=-1,0,-1,1,0,1,1,1,1,0,1,-1

19、,0,-1,-1,-1; /定义8个方向struct fang int x,y; int dir; /定义坐标和方向 class stack fang datamaxsize; int top; public: stack()top=0; /构造函数,给top赋初值 bool empty() /定义函数,用来确定栈是否为空 if(top=0) return true; else return false; void push(fang x); fang pop(); void print(); ;void stack:push(fang it) /压栈函数 if(top=maxsize) co

20、utstack is full!endl; return; datatop.x=it.x; datatop.y=it.y; datatop.dir=it.dir; top+;fang stack:pop() /出栈函数 if(top=0) coutstack is underflow!endl; return data0; top-; return datatop; void stack:print() /输出栈的函数 couttop=topendl; for(int i=0;itop;i+) coutdatai.x,datai.yendl; bool markmn; /定义数组用来标记走过的

21、路int mazemn; /迷宫数组void path(int m,int n) mark00=true; /为第一步标记已走过 fang temp=0,0,2; stack mazepath; mazepath.push(temp); /第一步先压栈while(!mazepath.empty() /当栈不为空时做循环 temp=mazepath.pop(); /先读出第一步 int i=temp.x; int j=temp.y; int d=temp.dir; while(d8) /8个方向没走完就做循环 int g=i+moved.a; /将下一步的x坐标赋给g int h=j+moved.b; /将下一步的y坐标赋给h if(g0)|(h=m)|(h=n) d+; /没走出界就继续走 else if(g=m-1)&(h=n-1) /如果走到最后了 temp.x=i; temp.y=j; temp.dir=d; mazepath.push(temp); /先压栈 mazepath.print(); /输出路径 coutm-1,n-1endl; /输出最后一步,既出口 return; if(!mazegh)&(!markgh) /如果没走过和标为0可以走的 markgh=true; temp.x=i; tem

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1