实践考试试题及答案Word下载.docx
- 文档编号:16101470
- 上传时间:2022-11-19
- 格式:DOCX
- 页数:13
- 大小:151.52KB
实践考试试题及答案Word下载.docx
《实践考试试题及答案Word下载.docx》由会员分享,可在线阅读,更多相关《实践考试试题及答案Word下载.docx(13页珍藏版)》请在冰豆网上搜索。
getfrombuffer1;
V(avail1);
P(avail2);
puttobuffer2;
V(full2);
gotoL2;
PC:
L3:
P(full2);
getfrombuffer2;
V(avail2);
printRECORD;
gotoL3
end;
Cobegin
PA;
PB;
PC;
Coend.
Java
1、用java语言编写一个java应用程序根据给定图实现最小生成树(MinimalSpinningTree),可以采用Prim算法和Kruskal算法,并用动画的方式表示最小生成树的生成过程。
importjava.util.*;
publicclassMain{
staticintMAXCOST=Integer.MAX_VALUE;
staticintPrim(intgraph[][],intn){
/*lowcost[i]记录以i为终点的边的最小权值,当lowcost[i]=0时表示终点i加入生成树*/
intlowcost[]=newint[n+1];
/*mst[i]记录对应lowcost[i]的起点,当mst[i]=0时表示起点i加入生成树*/
intmst[]=newint[n+1];
intmin,minid,sum=0;
/*默认选择1号节点加入生成树,从2号节点开始初始化*/
for(inti=2;
i<
=n;
i++){
/*最短距离初始化为其他节点到1号节点的距离*/
lowcost[i]=graph[1][i];
/*标记所有节点的起点皆为默认的1号节点*/
mst[i]=1;
}
/*标记1号节点加入生成树*/
mst[1]=0;
/*n个节点至少需要n-1条边构成最小生成树*/
min=MAXCOST;
minid=0;
/*找满足条件的最小权值边的节点minid*/
for(intj=2;
j<
j++){
/*边权值较小且不在生成树中*/
if(lowcost[j]<
min&
&
lowcost[j]!
=0){
min=lowcost[j];
minid=j;
/*输出生成树边的信息:
起点,终点,权值*/
System.out.printf("
%c-%c:
%d\n"
mst[minid]+'
A'
-1,minid+'
-1,min);
/*累加权值*/
sum+=min;
/*标记节点minid加入生成树*/
lowcost[minid]=0;
/*更新当前节点minid到其他节点的权值*/
/*发现更小的权值*/
if(graph[minid][j]<
lowcost[j]){
/*更新权值信息*/
lowcost[j]=graph[minid][j];
/*更新最小权值边的起点*/
mst[j]=minid;
/*返回最小权值和*/
returnsum;
publicstaticvoidmain(Stringargs[]){
Scannersc=newScanner(System.in);
intcost;
charchx,chy;
/*读取节点和边的数目*/
intn=sc.nextInt();
//节点
intm=sc.nextInt();
//边数
intgraph[][]=newint[n+1][n+1];
/*初始化图,所有节点间距离为无穷大*/
for(inti=1;
for(intj=1;
graph[i][j]=MAXCOST;
/*读取边信息*/
for(intk=0;
k<
m;
k++){
chx=sc.next().charAt(0);
chy=sc.next().charAt(0);
cost=sc.nextInt();
inti=chx-'
+1;
intj=chy-'
graph[i][j]=cost;
graph[j][i]=cost;
/*求解最小生成树*/
cost=Prim(graph,n);
/*输出最小权值和*/
System.out.println("
Total:
"
+cost);
}
2、编写一个java程序实现Min堆(Heap)或者Max堆的主要功能,并用动画的方式表示Min堆或者Max堆的变化过程。
publicclassMinHeap{
privateint[]Heap;
privateintmaxsize;
privateintsize;
publicMinHeap(intmax){
maxsize=max;
Heap=newint[maxsize];
size=0;
Heap[0]=Integer.MIN_VALUE;
privateintleftchild(intpos){
return2*pos;
privateintrightchild(intpos){
return2*pos+1;
privateintparent(intpos){
returnpos/2;
privatebooleanisleaf(intpos){
return((pos>
size/2)&
(pos<
=size));
privatevoidswap(intpos1,intpos2){
inttmp;
tmp=Heap[pos1];
Heap[pos1]=Heap[pos2];
Heap[pos2]=tmp;
publicvoidinsert(intelem){
size++;
Heap[size]=elem;
intcurrent=size;
while(Heap[current]<
Heap[parent(current)]){
swap(current,parent(current));
current=parent(current);
}
publicvoidprint(){
inti;
for(i=1;
i<
=size;
i++)
System.out.print(Heap[i]+"
"
);
System.out.println();
publicintremovemin(){
swap(1,size);
size--;
if(size!
=0)
pushdown
(1);
returnHeap[size+1];
privatevoidpushdown(intposition){
intsmallestchild;
while(!
isleaf(position)){
smallestchild=leftchild(position);
if((smallestchild<
size)&
(Heap[smallestchild]>
Heap[smallestchild+1]))
smallestchild=smallestchild+1;
if(Heap[position]<
=Heap[smallestchild])return;
swap(position,smallestchild);
position=smallestchild;
3、编写一个求解图的最小周游路径的算法,并用动画的方式表示最小周游路径的生成过程。
路径的生成过程。
packagewjcsq;
classEdge{
charvexa;
charvexb;
intweight;
Edge(charvexa,charvexb,intweight){
this.vexa=vexa;
this.vexb=vexb;
this.weight=weight;
publicclassprim{
staticEdge[]e={newEdge('
a'
'
b'
2),newEdge('
c'
1),
newEdge('
d'
e'
9),
f'
4),newEdge('
g'
h'
9),newEdge('
i'
8),newEdge('
6),
j'
3),newEdge('
k'
7),
1),newEdge('
6)};
staticintw(intx,inty){
charfrom=(char)(x+97);
charto=(char)(y+97);
for(inti=0;
18;
i++){
if(e[i].vexa==from&
e[i].vexb==to){
returne[i].weight;
if(e[i].vexa==to&
e[i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实践 考试 试题 答案