实践考试试题及答案

上传人:l**** 文档编号:145300865 上传时间:2020-09-18 格式:DOC 页数:15 大小:188KB
返回 下载 相关 举报
实践考试试题及答案_第1页
第1页 / 共15页
实践考试试题及答案_第2页
第2页 / 共15页
实践考试试题及答案_第3页
第3页 / 共15页
实践考试试题及答案_第4页
第4页 / 共15页
实践考试试题及答案_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《实践考试试题及答案》由会员分享,可在线阅读,更多相关《实践考试试题及答案(15页珍藏版)》请在金锄头文库上搜索。

1、. . 操作系统1. 有3个进程PA、PB和PC协作解决文件打印问题:PA将文件记录从磁盘读入主存的缓冲区1,每执行一次读一个记录;PB将缓冲区1的存复制到缓冲区2,每执行一次复制一个记录;PC将缓冲区2的容打印出来,每执行一次打印一个记录,缓冲区的大小和一个记录大小一样,请用进程通讯或P.V操作方式来保证文件的正确打印。解:答案一答案二定义信号量:avail1 ,avail2 初始值1full1, full2 初始值0PA:beginL1:read from disk; P(avail1); put to buffer1; V(full1); goto L1;End;PB:beginL2:P

2、(full1); get from buffer1; V(avail1); P(avail2); put to buffer2; V(full2);goto L2;End;PC:beginL3: P(full2); get from buffer2; V(avail2); print RECORD; goto L3 end ;Cobegin PA;PB;PC;Coend.Java1、用java语言编写一个java应用程序根据给定图实现最小生成树(Minimal Spinning Tree),可以采用Prim算法和Kruskal算法,并用动画的方式表示最小生成树的生成过程。解:import ja

3、va.util.*;public class Main static int MAXCOST=Integer.MAX_VALUE; static int Prim(int graph, int n) /* lowcosti记录以i为终点的边的最小权值,当lowcosti=0时表示终点i加入生成树 */ int lowcost=new intn+1; /* msti记录对应lowcosti的起点,当msti=0时表示起点i加入生成树 */ int mst=new intn+1; int min, minid, sum = 0; /* 默认选择1号节点加入生成树,从2号节点开始初始化 */ for

4、 (int i = 2; i = n; i+)/* 最短距离初始化为其他节点到1号节点的距离 */lowcosti = graph1i; /* 标记所有节点的起点皆为默认的1号节点 */msti = 1; /* 标记1号节点加入生成树 */ mst1 = 0; /* n个节点至少需要n-1条边构成最小生成树 */ for (int i = 2; i = n; i+)min = MAXCOST;minid = 0; /* 找满足条件的最小权值边的节点minid */ for (int j = 2; j = n; j+) /* 边权值较小且不在生成树中 */ if (lowcostj min &

5、lowcostj != 0) min = lowcostj; minid = j; /* 输出生成树边的信息:起点,终点,权值 */System.out.printf(%c - %c : %dn, mstminid + A - 1, minid + A - 1, min); /* 累加权值 */ sum += min; /* 标记节点minid加入生成树 */ lowcostminid = 0; /* 更新当前节点minid到其他节点的权值 */ for (int j = 2; j = n; j+) /* 发现更小的权值 */ if (graphminidj lowcostj) /* 更新权值

6、信息 */ lowcostj = graphminidj; /* 更新最小权值边的起点 */ mstj = minid; /* 返回最小权值和 */return sum; public static void main(String args) Scanner sc=new Scanner(System.in); int cost; char chx, chy; /* 读取节点和边的数目 */ int n=sc.nextInt();/节点 int m=sc.nextInt();/边数 int graph=new intn+1n+1; /* 初始化图,所有节点间距离为无穷大 */ for (in

7、t i = 1; i = n; i+)for (int j = 1; j = n; j+)graphij = MAXCOST; /* 读取边信息 */ for (int k = 0; k size/2) & (pos = size); private void swap(int pos1, int pos2) int tmp; tmp = Heappos1; Heappos1 = Heappos2; Heappos2 = tmp; public void insert(int elem) size+; Heapsize = elem; int current = size; while (He

8、apcurrent Heapparent(current) swap(current, parent(current); current = parent(current); public void print() int i; for (i=1; i=size;i+) System.out.print(Heapi + ); System.out.println(); public int removemin() swap(1,size); size-; if (size != 0) pushdown(1); return Heapsize+1; private void pushdown(i

9、nt position) int smallestchild; while (!isleaf(position) smallestchild = leftchild(position); if (smallestchild Heapsmallestchild+1) smallestchild = smallestchild + 1; if (Heapposition = Heapsmallestchild) return; swap(position,smallestchild); position = smallestchild; 3、编写一个求解图的最小周游路径的算法,并用动画的方式表示最

10、小周游路径的生成过程。路径的生成过程。解:package wjcsq;class Edge char vexa; char vexb; int weight; Edge(char vexa, char vexb, int weight) this.vexa = vexa; this.vexb = vexb; this.weight = weight; public class prim static Edge e = new Edge(a, b, 2), new Edge(b, c, 1),new Edge(c, d, 2), new Edge(d, e, 9),new Edge(e, f, 4), new Edge

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 工作范文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号