程序设计艺术与方法-修订编选

上传人:l****6 文档编号:149389631 上传时间:2020-10-27 格式:PDF 页数:16 大小:174.77KB
返回 下载 相关 举报
程序设计艺术与方法-修订编选_第1页
第1页 / 共16页
程序设计艺术与方法-修订编选_第2页
第2页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《程序设计艺术与方法-修订编选》由会员分享,可在线阅读,更多相关《程序设计艺术与方法-修订编选(16页珍藏版)》请在金锄头文库上搜索。

1、程序设计艺术与方法程序设计艺术与方法 实验一实验一 STL 的熟悉与使用的熟悉与使用 1 实验目的 (1) 掌握 C+中 STL 的容器类的使用。 (2) 掌握 C+中 STL 的算法 类的使用。 2 试验设备 硬件环境:PC 计算机 软件环境: 操作系统:Windows 2000 / Windows XP / Linux 语言环境:Dev cpp / gnu c+ 3 试验内容 (1) 练习 vector 和 list 的使用。 定义一个空的 vector,元素类型为 int, 生成 10 个随机数插入到 vector 中,用迭代 器遍历 vector 并输出其中的元素值。在 vector

2、头部插入一个随机数,用迭代器遍历 vector 并输出其中的元素值。用泛型算法 find 查找某个随机数,如果找到便输出,否则将此数 插入 vector 尾部。用泛型算法 sort 将 vector 排序,用迭代器遍历 vector 并输出其中的元 素值。删除 vector 尾部的元素, 用迭代器遍历 vector 并输出其中的元素值。将 vector 清 空。 定义一个 list,并重复上述 实验,并注意观察结果。 (2) 练习泛型算法的使用。 - 149 定义一个 vector,元素类型为 int,插入 10 个随机数,使用 sort 按升序排序,输 出 每个元素的值,再按降叙排序,输出每

3、个元素的值。练习用 find 查找元素。用 min 和 max 找出容器中的小元素个大元素,并输出。 源代码源代码: #include #include #include #include #include using namespace std; vector myV; bool sortup(int v1,int v2) return v1v2; int main(int argc, char *argv) srand(time(NULL); for (int i=0;i10;i+) myV.push_back(rand(); sort(myV.begin(),myV.end(),sort

4、up); vector:iterator it1; for (it1=myV.begin();it1!=myV.end();it1+) cout(*it1)setw(6); coutendl; int min=myV0; for (it1=myV.begin()+1;it1!=myV.end();it1+) if(*it1)min)min=(*it1); cout最小元素为 minmax)max=(*it1); cout最大元素为 maxendl; coutendl; int value=rand(); it1=find(myV.begin(),myV.end(),value); if(*it

5、1)=value) cout找到了这个随机数endl ; else cout没有找到这个随机数endl; myV.insert(myV.end(),value); cout插入尾部的随机数为valueendl; for (it1=myV.begin();it1!=myV.end();it1+) cout(*it1)setw(6); coutnendl; int t=rand(); myV.insert(myV.begin(),t); cout插入头部的随机数为 tendl; for (it1=myV.begin();it1!=myV.end();it1+) cout(*it1)setw(6);

6、 coutendl; myV.pop_back (); for (it1=myV.begin();it1!=myV.end();it1+) cout(*it1)setw(6); coutendl; myV.clear(); if(myV.empty() cout Its empty! endl; system(PAUSE); return 0; 运行截图:运行截图: 2 练习泛型算法的使用:练习泛型算法的使用: 源代码源代码: #include #include /#inclued using namespace std; typedef list lin; int value=1,2,3,4

7、,5; void print(lin lin:iterator lit; for(lit=l.begin();lit!=l.end();lit+) cout(*lit) ; coutv2; int main() lin lin2; lin2.push_front(3); lin2.push_front(4); lin2.insert(lin2.begin(),value,value+5); coutlin2 内的元素为:; print(lin2); lin2.sort(); cout排序后的 lin2: ; print(lin2); lin2.push_front(10); cout在 lis

8、t 头部插入 10 之后的结果:; print(lin2); lin2.remove(6); cout删除一个数后的 lin1:; print(lin2); system(PAUSE); return 0; 运行截图:运行截图: 实验二实验二 搜索算法的实现搜索算法的实现 1. 实验目的 (1) 掌握宽度优先搜索算法。 (2) 掌握深度优先搜索算法。 2. 试验设备 硬件环境:PC 计算机 软件环境: 操作系统:Windows 2000 / Windows XP / Linux 语言环境:Dev cpp / gnu c+ 3. 试验内容 (1) 将书上的走迷宫代码上机运行并检验结果,并注意体会

9、搜索的 思想。 (2) 八皇后问题:在一个国际象棋棋盘上放八个皇后,使得任何两个皇 后之间不相互攻击,求出所有的布棋方法。上机运行并检验结果。 思考: 将此题推广到 N 皇后的情况,检验在 N 比较大的情况下,比方说 N=16 的时 候,你的程序能否快速的求出结果,如果不能,思考有什么方法能够优化算法。 (3) 骑士游历问题: 在国际棋盘上使一个骑士遍历所有的格子一遍且仅一遍, 对于任意给定的顶点, 输出一条符合上述要求的路径。 (4) 倒水问题:给定 2 个没有刻度容器,对于任意给定的容积,求出如何只用两个瓶装出 L 升 的 水,如果可以,输出步骤,如果不可以,请输出 No Solution

10、。 (2)八皇后问题)八皇后问题 源代码:源代码: #include using namespace std; #include int sum = 0; int upperlimit = 1; void compare(int row,int ld,int rd) if(row!=upperlimit) int pos=upperlimit while(pos!=0) int p=pospos-=p; compare(row+p,(ld+p)1); elsesum+; int main() int n; coutn; upperlimit = (upperlimitn)-1; compare

11、(0,0,0); cout问题的解如下:sumendl; return 0; 运行截图:运行截图: (4)倒水问题)倒水问题 源代码:源代码: 4.倒水问题: #includestdio.h int main() int ca,cb,cc,x,y; while(scanf(%d%d%d, else if(ca=cc) printf(fill An); printf(pour A Bn); else x=y=0; if(caca-x) /如果 b 中的水大于 a 中的剩余容积,就把 a 灌满/ y-=ca-x; x=ca; printf(pour B An); else /如果 b 中的水小于

12、a 中的剩余容积,那么把 b 中的水全加入 a/ x+=y; y=0; printf(pour B An); if(y=cc) /如果 b 中的水已经和 cc 相等,那就结束/ break; if(ca=x) /如果 a 中的水满了,就把 a 倒空/ x=0; printf(empty An); else while(1) if(x=0) x=ca; printf(fill An); if(xcb-y) /如果 a 中的水大于 b 中的剩余容积,就把 b 灌满/ x-=cb-y; y=cb; printf(pour A Bn); else /如果 a 中的水小于 b 中的剩余容积,那么把 a 中的水全加入 b/ y+=x; x=0; printf(pour A Bn); if(y=cc) /如果 b 中的水已经和 cc 相等,那就结束/ break; if(y=cb) /如果 b 中的水满了,就把 b 倒空/ y=0; printf(empty Bn); print

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

当前位置:首页 > 办公文档 > PPT模板库 > 总结/计划/报告

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