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

上传人:b****7 文档编号:11197287 上传时间:2023-02-25 格式:DOCX 页数:78 大小:34.47KB
下载 相关 举报
C++面向对象 学生作品.docx_第1页
第1页 / 共78页
C++面向对象 学生作品.docx_第2页
第2页 / 共78页
C++面向对象 学生作品.docx_第3页
第3页 / 共78页
C++面向对象 学生作品.docx_第4页
第4页 / 共78页
C++面向对象 学生作品.docx_第5页
第5页 / 共78页
点击查看更多>>
下载资源
资源描述

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

《C++面向对象 学生作品.docx》由会员分享,可在线阅读,更多相关《C++面向对象 学生作品.docx(78页珍藏版)》请在冰豆网上搜索。

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

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:

贪心背包算法133

作品21:

贪心背包算法234

作品22:

贪心链表背包35

作品23:

学生成绩查询38

作品24:

医院挂号程序39

作品25:

用递归实现的皇后问题41

作品26:

用类的继承来实现的各种方法的积分43

作品27:

运算符重载45

作品1:

0-1背包动态规划

#include

#definen5

#defineW17

main()

{

intw,i;

intc[n+1][W+1];//背包容量为w时有i件可选物体所导致的最优解的总价值

intw1[n+1]={0,3,4,7,8,9};//物品重量

intv[n+1]={0,4,5,10,11,13};//物品价值

intx[n+1]={0};//初始的物品选择

//物品个数或背包容量为0时总价值都为0

for(w=0;w<=W;w++)

c[0][w]=0;

for(i=1;i<=n;i++)

c[i][0]=0;

//自底向上推

for(i=1;i<=n;i++)//按物品初始化时顺序依次尝试加入背包,初始化时顺序可变

for(w=1;w<=W;w++)

{

if(w1[i]<=w)

{

//c[i][w]是靠v[i]加起来的,正如矩阵链乘中是m[i][j]是靠矩阵下标加起来的

//可以用第i件,若用则用后前i-1件物品需重排,但预留重量仅为

//w-w1[i],由最优化原理知v1[i]+c[i-1][w-w1[i]]

//w-w1[i]大小未知,故w从1到W的情况都要考虑

if(v[i]+c[i-1][w-w1[i]]>c[i-1][w])

{

c[i][w]=v[i]+c[i-1][w-w1[i]];

}

else

c[i][w]=c[i-1][w];

//用不着第I件,前I-1件已满足条件,可能还有剩余

}

//c[i][w]=c[i-1][w]中不仅有i-1到i的传递,也包含i-2经i-1到i的传递

else

c[i][w]=c[i-1][w];

}

printf("%d\n",c[n][W]);

w=W;

for(i=n;i>=1;i--)//根据自底向上推时判断的过程回推

{

if(c[i][w]==c[i-1][w])

x[i]=0;

else

{

x[i]=1;

w=w-w1[i];

}

}

for(i=1;i<=5;i++)

printf("%d\n",x[i]);

getch();

}

作品2:

n皇后问题

#include"stdio.h"

#defineM10

intx[M];

boolplace(intk,intj)

{

inti;

for(i=1;i

{

if((x[i]==j)||(x[k-i]==j+i)||(x[k-i]==j-i))

returnfalse;

}

returntrue;

}

boolqueen(intk,intn)

{

intj;

for(j=1;j<=n;j++)

{

if(place(k,j)==true)

{

x[k]=j;

if(k==n)

returntrue;

else

if(queen(k+1,n)==true)

returntrue;

else

{

x[k]=0;

continue;

}

}

}

returnfalse;

}

voidmain()

{

intn,k;

printf("qingshuruqizidgeshu:

");

scanf("%d",&n);

for(k=1;k<=n;k++)

queen(k,n);

for(k=1;k<=n;k++)

printf("第%d行排在第%d列\n",k,x[k]);

}

作品3:

病人挂号系统

#include"iostream.h"

structpatient{intnum;charname[10];charsex[6];intage;patient*next;};

voidmain()

{patient*head=NULL,*tail=NULL;

patient*p,*q,*temp;

charchoice,choice2;

I1:

cout<<"挂号请输入A;"<

<<"插入请输入I;"<

<<"删除请输入D;"<

<<"就诊请输入B;"<

<<"查看状态请输入L;"<

<<"保存关机请输入S."<

<<"请选择:

";

while

(1)

{cin>>choice;

if(choice=='A')//添加

I2:

{p=newpatient;

cout<<"请依次输入要添加病人的资料:

";

cin>>p->num>>p->name>>p->sex>>p->age;

p->next=NULL;

if(head==NULL)

{head=p;tail=p;}

else{tail->next=p;tail=p;}

cout<<"添加成功!

"<

<<"输入下一个请按C,跳出请按J.";

cin>>choice2;

if(choice2=='J')

{break;}

elseif(choice2=='C')

{gotoI2;}

}

elseif(choice=='I')//插入

{temp=newpatient;

cout<<"请依次输入要插入病人的资料:

";

cin>>temp->num>>temp->name>>temp->sex>>temp->age;

inti_num;

cout<<"请输入(要插入到哪一个号码后面):

";

cin>>i_num;

p=head;

while

(1)

{if(p->num!

=i_num)

{q=p;p=p->next;}

elsebreak;

}

if(p==NULL)

{cout<<"查找失败";}

else

{q=p;

p->next=temp;

temp->next=q->next;

tail=temp;}

cout<<"插入成功,请执行其他操作."<

gotoI1;

}

elseif(choice=='D')//删除

{cout<<"请输入要删除的号码:

";

intd_num;

cin>>d_num;

p=head;

while

(1)

{if(p->num!

=d_num)

{q=p;p=p->next;}

elsebreak;

}

if(p==NULL)

{cout<<"查找失败";}

else

{q->next=p->next;

delete[]p;}

cout<<"删除成功,请执行其他操作."<

gotoI1;

}

elseif(choice=='B')//就诊

{if(head==tail==NULL)

{cout<<"error";}

else{cout<num<name;

p=head;head=head->next;

deletep;

cout<<"请下一位病人做好准备.";}

}

elseif(choice=='L')//查看状态

{p=head;

cout<<"当前列表状态如下:

"<

while(p!

=NULL)

{cout<num<<""<name<<""<sex<<""<age<

p=p->next;}

cout<<"请继续执行其他操作:

"<

gotoI1;

}

elseif(choice=='S')

{cout<<"保存关机";

break;

}}}

作品4:

挂号程序

#include

structpatient

{intnum,age,sex;charname[20];patient*next;};

voidmain()

{patient*head=NULL,*tail=NULL,*p,*q;charch;intnum,n,age,sex;

cout<<"挂号请按A"<

cout<<"治病请按B"<

cout<<"查看请按C"<

cout<<"跳出请按D"<

cout<<"删除号码请按E"<

cout<<"插入请按F"<

while

(1)

{cin>>ch;

if(ch=='A')

{p=newpatient;

cout<<"请输入病人的信息"<

cin>>p->num>>p->name>>p->age>>p->sex;//挂号

p->next=NULL;

if(head==tail==NULL)

{head=p;tail=p;}

else{tail->next=p;tail=p;}

cout<<"完成,可以进行其他操作";}

elseif(ch=='B')

{if(head==tail==NULL)

cout<<"error"<

else{cout<num<name<age<sex;//治病

p=head;head=head->next;

deletep;

cout<<"完成,可以进行其他操作";}}

elseif(ch=='C')

{p=head;

while(p!

=NULL)//查看

{cout<num<name<age<sex<

p=p->next;

cout<<"完成,可以进行其他操作";}}

if(ch=='E')

{cout<<"请输入要删除的号码";

intd;

cin>>d;//删除

p=head;

while

(1)

{if(p->num!

=d)

{q=p;p=p->next;}}

if(p=NULL)

{cout<<"查无此人";}

else{q->next=p->next;deletep;}

}

if(ch=='F')

{p=newpatient;

cout<<"请输入要插入的号码";

intd;

cin>>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;num

num=num+1;

cout<<"请输入插入者的信息";

cin>>p->num>>p->name>>p->age>>sex;}

elseif(ch=='D')

break;}//跳出

}

作品5:

递归的二分查找

#include

search(int*p,intv,intlow,inthigh)

{

intmid;

while(low<=high)

{

mid=(low+high)/2;

if(v==p[mid])

returnmid;

elseif(v>p[mid])

low=mid+1;

elsehigh=mid-1;

}

return0;

}

main()

{

inti,j,k,temp,a[11],v,t;

printf("请输入一组数字:

\n");

for(i=1;i<11;i++)

scanf("%d",&a[i]);

for(i=1;i<11-1;i++)

{

k=i;

for(j=i+1;j<11;j++)

if(a[j]

k=j;

temp=a[k];

a[k]=a[i];

a[i]=temp;

}

printf("请输入v值:

\n");

scanf("%d",&v);

t=search(a,v,1,10);

printf("数组排好序后v值在数组中的位置是:

%d\n",t);

}

作品6:

动态规划最短路径

//用动态规划求两点之间的最短路径

#include"stdafx.h"

#include"iostream.h"

#defineM2//分成2段

#defineK3//各段中得最多节点数

#defineN4

intgraph[N][N];//邻接矩阵

intseg[M+1][K];//分段信息

intcost[N+1];//节点到终点的最短距离

intd[M];//最优路径下的下一个节点

ints[M+1];//每一段的节点数

voidcall(inth,int&p)//计算

{

inti,j,T,tag=0;

//赋值

cout<<"最后一段各结点到终点的距离!

"<

for(i=0;i

{

cost[seg[M-1][i]]=graph[seg[M-1][i]][N-1];

cout<

}

cout<

//********************

for(i=0;i

{

for(j=0;j

{

if(tag=0&&graph[seg[h][i]][seg[h+1][j]])

{T=graph[seg[h][i]][seg[h+1][j]]+

cost[seg[h+1][j]];

tag=1;

}

}

//8888888888888888888888888888

if(tag==1)

break;

}

if(tag==0)

{

cout<<"无路可走!

"<

return;

}

//888888888888888888888888888

//*******************************

for(;i

{

for(;j

{

if(T>graph[seg[h][i]][seg[h+1][j]]

+cost[seg[h+1][j]])

{cost[seg[h][i]]=graph[seg[h][i]][seg[h+1][j]];

p=seg[h][i];}

}

}

//*************************************************8

}

voidmain()

{

inti,j,sum=0;

cost[N]=0;

cout<<"请输入邻接矩阵!

"<

for(i=0;i

for(j=0;j

cin>>graph[i][j];

cout<<"请输入每段的结点数!

"<

for(i=0;i<=M;i++)

cin>>s[i];

cout<<"每段的结点数!

"<

for(i=0;i

cout<

cout<

seg[0][0]=0;

for(i=1;i<=M;i++)

{sum+=s[i-1];

for(j=0;j

seg[i][j]=sum+j;

}

cout<<"每个结点的关键字为!

"<

for(i=0;i<=M;i++)

for(j=0;j

cout<

cout<

for(i=M-1;i>=0;i--)

call(i,d[i]);

cout<<"最优路径为!

"<

for(i=0;i

cout<";

cout<

}

作品7:

非递归的二分查找

//输入十个数,然后用二分查找法从中查找一个数

#include

main()

{

inta[11],i,j,temp,mid,high,low,b;

printf("Input10numbers:

\n");

for(i=1;i<11;i++)

scanf("%d",&a[i]);//输入十个数

for(i=1;i<11;i++)//选择排序这十个数

{

for(j=i+1;j<11;j++)

{

if(a[i]>a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

for(j=1;j<11;j++)

printf("thesortednumbers:

%d\n",a[j]);

printf("");

scanf("%d",&b);//输入要查找的数

low=1;

high=10;

while(low<=high)//二分查找

{

mid=(low+high)/2;

if(b==a[mid])

{

returnmid;

printf("thea[mid]isfound/n");//找到即输出

}

else

{

if(b>a[mid])

low=mid+1;

else

high=mid-1;

}

}

printf("thenumber%disnotfound\n",b);//未找到

}

作品8:

非递归实现的迷宫问题

#include"iostream.h"

constintm=4,n=4;

constintmaxsize=m*n;//定义迷宫大小

structoff{inta,b;};//定义方向

constoffmove[8]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};//定义8个方向

structfang

{intx,y;

intdir;

};//定义坐标和方向

classstack

{fangdata[maxsize];

inttop;

public:

stack(){top=0;}//构造函数,给top赋初值

boolempty()//定义函数,用来确定栈是否为空

{if(top==0)

returntrue;

else

returnfalse;

}

voidpush(fangx);

fangpop();

voidprint();

};

voidstack:

:

push(fangit)//压栈函数

{if(top==maxsize)

{cout<<"stackisfull!

"<

return;

}

data[top].x=it.x;

data[top].y=it.y;

data[top].dir=it.dir;

top++;

}

fangstack:

:

pop()//出栈函数

{if(top==0)

{cout<<"stackisunderflow!

"<

returndata[0];

}

top--;

returndata[top];

}

voidstack:

:

print()//输出栈的函数

{cout<<"top="<top<

for(inti=0;i

cout<

}

boolmark[m][n];//定义数组用来标记走过的路

intmaze[m][n];//迷宫数组

voidpath(intm,intn)

{mark[0][0]=true;//为第一步标记已走过

fangtemp={0,0,2};

stackmazepath;

mazepath.push(temp);//第一步先压栈

while(!

mazepath.empty())//当栈不为空时做循环

{temp=mazepath.pop();//先读出第一步

inti=temp.x;

intj=temp.y;

intd=temp.dir;

while(d<8)//8个方向没走完就做循环

{intg=i+move[d].a;//将下一步的x坐标赋给g

inth=j+move[d].b;//将下一步的y坐标赋给h

if((g<0)||(h<0)||(g>=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();//输出路径

cout<

return;

}

if((!

maze[g][h])&&(!

mark[g][h]))//如果没走过和标为0可以走的

{mark[g][h]=true;

temp.x=i;

tem

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

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

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