20个超经典计算机考研机试题目及源码.pdf_第1页
20个超经典计算机考研机试题目及源码.pdf_第2页
20个超经典计算机考研机试题目及源码.pdf_第3页
20个超经典计算机考研机试题目及源码.pdf_第4页
20个超经典计算机考研机试题目及源码.pdf_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

20 个超经典个超经典计算机考研机试计算机考研机试题目及题目及源码源码 对输入的 n 个数进行排序并且输出 p 8 test1 cpp 定义控制台应用程序的入口点 include stdafx h int tmain int argc TCHAR argv int n int buf 100 定义我们将要使用的变量 n 并用 buf 100 来保存将要排序的数字 while scanf d i n i scanf d 输入待排序数字 for int i 0 i n i for int j 0 j buf j 1 int tem buf j buf j buf j 1 buf j 1 tem 冒泡排序主体 for int i 0 i n i printf d buf i printf n 输出换行 return 0 快速排序算法 利用库函数实现 p 10 test2 cpp 定义控制台应用程序的入口点 include stdafx h include algorithm 快速排序 库函数 using namespace std int tmain int argc TCHAR argv int n int buf 1000 while scanf d i n i scanf d sort buf buf n for int i 0 iy int tmain int argc TCHAR argv int n int buf 100 while scanf d i n i scanf d sort buf buf n cmp for int i 0 i n i printf d buf i printf n return 0 对学生成绩进行排序 p12 test4 cpp 定义控制台应用程序的入口点 include stdafx h include algorithm include string h using namespace std struct E char name 101 int age int score buf 100 bool cmp E a E b if a score b score return a score b score int tmp strcmp a name b name if tmp 0 return tmp 0 else return a age b age int tmain int argc TCHAR argv int n while scanf d i n i scanf s d d sort buf buf n cmp for int i 0 i n i printf s d d n buf i name buf i age buf i score 输出排序后的结果 return 0 对学生的成绩及其信息进行排序 对 进行重载从而实现排序 p13 test5 cpp 定义控制台应用程序的入口点 include stdafx h include algorithm include string h using namespace std struct E char name 101 int age int score bool operator const E int tmp strcmp name b name if tmp 0 return tmp 0 else return age b age buf 1000 int tmain int argc TCHAR argv int n while scanf d i n i scanf s d d sort buf buf n for int i 0 i dayOfMonth Month ISYEAP Year Day 1 Month if Month 12 Month 1 Year int buf 5001 13 32 int Abs int x return x dayOfMonth Month ISYEAP Year Day 1 Month if Month 12 Month 1 Year int buf 3001 13 32 char monthName 13 20 January February March April May June July August September October November December char weekName 7 20 Sunday Monday Tuesday Wednesday Thursday Friday Saturday 周名 每个周名对应下标 0 6 int tmain int argc TCHAR argv Date tmp int cnt 0 tmp Day 1 tmp Month 1 tmp Year 0 while tmp Year 3001 buf tmp Year tmp Month tmp Day cnt tmp nextDay cnt int d m y char s 20 while scanf d s d m 12 m if strcmp s monthName m 0 break int days buf y m d buf 2012 7 16 days 1 puts weekName days 7 7 7 return 0 统计同成绩学生人数 利用 Hash 表实现 Hash 表实则是一个数 组 p22 test8 cpp 定义控制台应用程序的入口点 include stdafx h int tmain int argc TCHAR argv int n while scanf d for int i 0 i n i int x scanf d Hash x int x scanf d printf d n Hash x return 0 给你 n 个整数 请按从大到小的顺序输出其中前 m 大的数 p22 test9 cpp 定义控制台应用程序的入口点 include stdafx h define OFFSET 500000 偏移量 用于补偿实际数字与数组 下标之间的偏移 int Hash 1000001 int tmain int argc TCHAR argv int n m while scanf d d i 500000 i Hash i OFFSET 0 for int i 0 i 500000 i if Hash i OFFSET 1 printf d i m if m 0 printf else printf n break return 0 输出梯形 考察考生对于输出格式的把握 p26 test10 cpp 定义控制台应用程序的入口点 include stdafx h int tmain int argc TCHAR argv int h while scanf d for int i 1 i h i for int j 1 j maxLine j if j maxLine h i 1 2 1 printf else printf printf n return 0 查找并输出数组的下标 p31 test11 cpp 定义控制台应用程序的入口点 include stdafx h int tmain int argc TCHAR argv int buf 200 int n while scanf d i n i scanf d int x ans 1 scanf d for int i 0 i n i if x buf i ans i break printf d n ans return 0 输入 n 个学生的信息 然后进行查询 p34 test12 cpp 定义控制台应用程序的入口点 include stdafx h include algorithm include string h using namespace std struct Student char no 100 char name 100 int age char sex 5 bool operator const Student buf 100 int tmain int argc TCHAR argv int n while scanf d i base int mid top base 2 int tmp strcmp buf mid no x if tmp 0 ans mid break else if tmp 0 top mid 1 else base mid 1 if ans 1 printf No Answer n else printf s s s d n buf ans no buf ans name buf ans sex buf ans age return 0 运用贪心算法解决购买最高性价比的物品 p38 test13 cpp 定义控制台应用程序的入口点 include stdafx h include algorithm using namespace std struct goods double j 该物品总重 double f 该物品总价值 double s 该物品性价比 bool operator A s buf 100 int tmain int argc TCHAR argv double m int n while scanf lf d for int i 0 i0 m buf idx f else ans buf idx j m buf idx f m 0 idx printf 3lf n ans return 0 尽可能地收看更多地电视节目 使用贪心算法实现 p40 test14 cpp 定义控制台应用程序的入口点 include stdafx h include algorithm using namespace std struct program int startTime 节目开始时间 int endTime 节目结束时间 bool operator const program buf 100 int tmain int argc TCHAR argv int n while scanf d for int i 0 i n i scanf d d sort buf buf n int currentTime 0 ans 0 for int i 0 i n i if currentTime buf i startTime currentTime buf i endTime ans printf d n ans return 0 括号匹配问题 使用库函数中的栈来解决 p43 test15 cpp 定义控制台应用程序的入口点 include stdafx h include using namespace std stack S 定义一个堆栈 char str 110 保存输入字符串 char ans 110 保存输出字符串 int tmain int argc TCHAR argv while scanf s str EOF int i for i 0 str i 0 i 从左到右遍历字符串 if str i S push i ans i else if str i if S empty false S pop ans i else ans i else ans i while S empty ans S top S pop ans i 0 puts str puts ans return 0 利用堆栈对表达式求值 利用两个栈来实现 一个用来存放运算 符 另一个用来保存数字 p45 test16 cpp 定义控制台应用程序的入口点 include stdafx h include using namespace std char str 220 int mat 5 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 1 1 1 0 0 stack op 运算符栈 保存运算符编号 stack in 数字栈 运算结果可能存在浮点数 所以保存 元素为 double void getOp bool retn 0 编号为 0 return if str i 0 reto true retn 0 return if str i 0 返回为数字 else reto true 返回为运算符 if str i retn 1 else if str i retn 2 else if str i retn 3 else if str i retn 4 i 2 return retn 0 for str i i retn 10 retn str i 0 if str i i return int tmain int argc TCHAR argv while gets str if str 0 0 bool retop int retnum int idx 0 while op empty op pop while in empty in pop 清空数字栈和运算栈 while true getOp retop retnum idx if retop false in push double retnum else double tmp if op empty true mat retnum op top 1 op push retnum else while mat retnum op top 0 int ret op top op pop double b in top in pop double a in top in pop if ret 1 tmp a b else if ret 2 tmp a b else if ret 3 tmp a b else tmp a b in push tmp op push retnum if op size 2 printf 2f n in top return 0 构造哈夫曼树 使用优先队列构造小顶堆 p49 test17 cpp 定义控制台应用程序的入口点 include stdafx h include using namespace std priority queue int vector greater Q 建立一个小 顶堆 int tmain int argc TCHAR argv int n while scanf d 清空堆中元素 for int i 1 i1 int a Q top Q pop int b Q top Q pop ans a b Q push a b printf d n ans return 0 二叉排序树 建立二叉排序树并进行前序中序后序遍历 p56 test19 cpp 定义控制台应用程序的入口点 include stdafx h include string h struct Node Node lchild Node rchild int c Tree 100 int loc Node creat Tree loc lchild Tree loc rchild NULL return void postOrder Node T if T lchild NULL postOrder T lchild if T rchild NULL postOrder T rchild printf d T c void inOrder Node T if T lchild NULL inOrder T lchild printf d T c if T rchild NULL inOrder T rchild void preOrder Node T printf d T c if T lchild NULL preOrder T lchild if T rchild NULL preOrder T rchild Node Insert Node T int x if T NULL T creat T c x return T else if x c T lchild Insert T lchild x else if x T c T rchild Insert T rchild x return T int tmain int argc TCHAR

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论