随笔分类 - PTA
摘要:解题思路:结构体排序 按题目要求排序,再按年龄段和最大输出数目输出 #include <stdio.h> #include <string.h> typedef char Element[9]; typedef struct { Element Name; int age,wealth; } Bil
阅读全文
摘要:解题思路: 题目要求: 1、按总分降序,若总分相等,则按题目满分数目降序,若满分题目数目相等,则按学号升序 2、若全部题目均是未提交或者提交未通过,不输出 #include <stdio.h> #define MaxN 10001 #define MaxK 5+1 typedef struct{ i
阅读全文
摘要:解题思路:入栈顺序即为前序,出栈顺序为中序,等价于求由前序和中序确定后序 #include <stdio.h> #include <string.h> #include <malloc.h> typedef char Element[5]; typedef struct TNode { int da
阅读全文
摘要:解题思路:寻找最大子列和,并输出最大子列和的第一个数和最后一个数 #include <stdio.h> int main(int argc,char **agrv) { int n; int i; scanf("%d",&n); int a[n]; int b[2]; int sum=0; int
阅读全文
摘要:解题思路: 1、结点地址是固定5位整数,故可用数组按结点地址存放 2、利用辅助数组存放排好序的链表(不在链表上的多余结点不处理),当k=1时,链表不翻转,k>1时按要求翻转链表 #include <stdio.h> #define Max 100000 typedef struct { int Ad
阅读全文
摘要:解题思路: 1、注意到结点地址是固定5位整数,故输入数据可以按地址存放。 2、键值<=10000,故可用辅助数组按键值标记重复出现的键值 3、用两个辅助数组分别存放去重后的链表、被删除的链表 4、分别打印 #include <stdio.h> #define Max 100000 typedef s
阅读全文
摘要:解题思路:注意时间复杂度要求是O(n) void MySort( ElementType A[], int N ) { int i; ElementType b[3]= {0}; for(i=0; i<N; i++) { b[A[i]]++; } i=0; while(b[1]--) A[i++]=
阅读全文
摘要:解题思路:小顶堆之向上高速+向下调整 void PercolateUp( int p, PriorityQueue H ){ H->Elements[0]=H->Elements[p]; int i; for(i=p/2;i>0;i/=2) { if(H->Elements[0]>=H->Eleme
阅读全文
摘要:解题思路:递归判断 int Isomorphic( Tree T1, Tree T2 ) { if(!T1&&!T2) return 1; else if(T1&&!T2) return 0; else if(!T1&&T2) return 0; else { if(T1->Element==T2-
阅读全文
摘要:解题思路:计算后缀表达式的值 1、遇到数字入栈 2、遇到符号则出栈计算 3、或栈中仅剩1个数,则栈中值即为所求,否则,错误 ElementType EvalPostfix( char *expr ) { ElementType stack[Max_Expr]; char b[Max_Expr]; i
阅读全文
摘要:解题思路:(此前用哈希表存储学生选课信息,最后一个测试点超时,或者内存超限) 后在网上翻看其他大能写的文章,受益颇多 注意到学生姓名的组成是3个大写字母+1个数字,故可开辟一个四维结构体数组指针,使学生姓名映射到唯一地址,也省去了用哈希函数要解决冲突的时间消耗问题 解法一、将学生的选课清单用二叉排序
阅读全文
摘要:解题思路:采用桶排序思想,将每个课程看成一个桶,再把每个课程对应学生放入桶,其中学生名单的采用二叉排序树结构存储 #include <stdio.h> #include <malloc.h> #include <string.h> #define MaxSize 2503 #define MaxS
阅读全文
摘要:解题思路: 1、存储:用一张哈希表存储单词以及对应所在的文件,再用一张文件表,存储每个文件的词汇量以及单词在哈希表中的位置 2、查询:先在文件表中查询对应的文件名,(取文件词汇量较少的文件名)-> 找到对应文件名中的词汇所在位置-> 根据此单词的位置到哈希表中查找单词所在文件列表->从而判断该单词是
阅读全文
摘要:解题思路: 1、存储:用哈希存储单词,并用文件链表记录单词所在文件,以及行号链表记录单词所在文件中的行号 2、查询:取各单词文件名交集,再取各文件名再行号并集 #include <stdio.h> #include <string.h> #include <malloc.h> #include <c
阅读全文
摘要:解题思路:(注意此题我没有特意处理大小写字符问题都能AC,应该是测试用例没有特意测试大小写) 解法一、将输入数据存成二叉排序树,再中序遍历输出即可 #include <stdio.h> #include <malloc.h> #include <string.h> typedef char Elem
阅读全文
摘要:解题思路:并查集思想 #include <stdio.h> #define Max 30000+1 int book[Max]; int f[Max]; int n,m; int getf(int x) { if(f[x]==x) return x; else return f[x]=getf(f[
阅读全文
摘要:解题思路: 1、先按第一跳距离升序 2、合法第一跳加入队列中 3、(广度优先)访问队列中的结点,每访问一个结点并将其可到达的子孙加入队列中,直至访问至某个结点可以到岸结束 #include <stdio.h> #include <string.h> #include <math.h> #define
阅读全文
摘要:解题思路:采用DFS算法判断007是否能够逃生,每次先判断是否能够上岸,如若不能上岸再选取下一跳的落脚点,注意到第一跳的半径是D+7.5,而其他跳半径为D #include <stdio.h> #include <math.h> #include <string.h> int tag=0; int
阅读全文
摘要:解题思路: 由于哈夫曼树并不唯一,但哈夫曼树的带权路径长度 WPL是相同且是最优, 故,利用哈夫曼算法求出WPL,再计算每套编码的WPL,如果WPL相同,则判断各字符编码是否是其他字符编码的前缀 #include <stdio.h> #include <string.h> #define INF 0
阅读全文
摘要:解题思路:采用将多叉树转化为二叉树的方法(即利用孩子兄弟的结点结构)建立二叉树,最后再先序遍历输出即可 #include <stdio.h> #include <malloc.h> #include <string.h> struct TNode { int space;//记录打印空格数 int
阅读全文

浙公网安备 33010602011771号