
PTA
沿麓
coder
展开
-
PTA 1001 A+B Format
#include <iostream>#include <string>using namespace std;int main(int argc, const char * argv[]) { int a,b; int sum; string str; scanf("%d %d",&a,&b); sum=a...原创 2018-10-14 22:30:15 · 203 阅读 · 0 评论 -
6-3 邻接表存储图的广度优先遍历 (20 分)
试实现邻接表存储图的广度优先遍历。函数接口定义:void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) );其中LGraph是邻接表存储的图,定义如下:/* 邻接点的定义 */typedef struct AdjVNode *PtrToAdjVNode; struct AdjVNode{ Vertex Ad...原创 2018-12-19 17:18:47 · 3315 阅读 · 0 评论 -
6-2 图的深度遍历-邻接表实现 (10 分)
本题要求实现邻接表存储图的深度优先遍历。函数接口定义:void DFS(ALGraph *G,int i);其中ALGraph是邻接表存储的图,定义如下:#define MAX_VERTEX_NUM 10 /*定义最大顶点数*/typedef int Vertex;typedef struct ArcNode{ /*表结点*/ int adjv...原创 2018-12-19 17:19:32 · 10207 阅读 · 0 评论 -
6-1 邻接矩阵存储图的深度优先遍历 (20 分)
试实现邻接矩阵存储图的深度优先遍历。函数接口定义:void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) );其中MGraph是邻接矩阵存储的图,定义如下:typedef struct GNode *PtrToGNode;struct GNode{ int Nv; /* 顶点数 */ int Ne; ...原创 2018-12-19 17:20:16 · 5239 阅读 · 0 评论 -
7-2 Insert or Merge (25 分)
According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data,...原创 2018-12-14 19:03:00 · 609 阅读 · 0 评论 -
7-10 PAT排名汇总 (25 分)
计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科学的评价计算机程序设计人才,为企业选拔人才提供参考标准(网址http://www.patest.cn)。每次考试会在若干个不同的考点同时举行,每个考点用局域网,产生本考点的成绩。考试结束后,各个考点的成绩将即刻汇总成一张总的...原创 2018-12-15 20:10:33 · 1434 阅读 · 1 评论 -
7-12 抢红包 (25 分)
没有人没抢过红包吧…… 这里给出N个人之间互相发红包、抢红包的记录,请你统计一下他们抢红包的收获。输入格式:输入第一行给出一个正整数N(≤104),即参与发红包和抢红包的总人数,则这些人从1到N编号。随后N行,第i行给出编号为i的人发红包的记录,格式如下:KN1P1⋯NKPK其中K(0≤K≤20)是发出去的红包个数,Ni是抢到红包的人的编号,P...原创 2018-12-15 20:50:26 · 743 阅读 · 0 评论 -
6-2 LCA in BST (25 分)
The lowest common ancestor (LCA) of two nodes u and v in a tree T is the deepest node that has both u and v as descendants. Given any two nodes in a binary search tree (BST), you are supposed to find ...原创 2018-12-10 14:39:07 · 444 阅读 · 1 评论 -
6-3 AVL Insertion (30 分)
You are supposed to implement the Insert function, which inserts an integer Key into an AVL tree T. The resulting tree must be returned.Format of function:AVLTree Insert ( AVLTree T, int Key );...原创 2018-12-10 14:51:03 · 3035 阅读 · 4 评论 -
7-2 字符串关键字的散列映射 (25 分)
7-2 字符串关键字的散列映射 (25 分)给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位;再用除留余数法将整数映射到长度为P的散列表中。例如将字符串AZDEG插入长度为1009的散列表中,我们首先将26个大写英文字母顺序映射到整数0~25;再通过移位将其映射为3×322+4×32+6=320...原创 2018-12-10 15:01:32 · 1799 阅读 · 0 评论 -
6-5 No Greater Than X in BST (20 分)
6-5 No Greater Than X in BST (20 分)You are supposed to output, in decreasing order, all the elements no greater than X in a binary search tree T.Format of function:void Print_NGT( Tree T, int ...原创 2018-12-10 15:02:57 · 1063 阅读 · 0 评论 -
7-1 Maximum Subsequence Sum (25 分)
Given a sequence ofKintegers {N1,N2, ...,NK}. A continuous subsequence is defined to be {Ni,Ni+1, ...,Nj} where1≤i≤j≤K. The Maximum Subsequence is the continuous subsequen...原创 2019-04-22 15:29:31 · 412 阅读 · 0 评论 -
6-2 Iterative Mergesort (25 分)
How would you implement mergesort without using recursion?The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one...原创 2018-12-13 23:10:27 · 2777 阅读 · 0 评论 -
6-8 Percolate Up and Down (20 分)
Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap.Format of functions:void PercolateUp( int p, PriorityQueue H );void PercolateDown( int p, PriorityQueue H );...原创 2018-12-03 19:39:07 · 1607 阅读 · 0 评论 -
PTA 1003 Emergency
#include <iostream>#define MAX 501int main(int argc, const char * argv[]) { int N,M,C1,C2; int numOfTeam[MAX]; int e[MAX][MAX]; scanf("%d %d %d %d",&N,&M,&C1,&...原创 2018-10-14 22:33:09 · 285 阅读 · 0 评论 -
PTA 1002 A+B for Polynomials
#include <stdio.h>#include <math.h>typedef struct Poly{ double coef; int exp;} Poly;int main(){ int KA, KB, Ksum = 0; Poly A[20], B[20], sum[20]; scanf("%d...原创 2018-10-14 22:37:47 · 457 阅读 · 0 评论 -
7-1 Pop Sequence (25 分)
7-1 Pop Sequence (25 分)Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a p...原创 2018-10-22 16:30:49 · 1962 阅读 · 0 评论 -
习题1.9 有序数组的插入 (20 分)
bool Insert( List L, ElementType X ){ if(L->Last+1==MAXSIZE) return false; for (int i=0; i<=L->Last; i++) { if(L->Data[i]==X) return false; els...原创 2018-10-28 14:42:14 · 8723 阅读 · 0 评论 -
PTA打印沙漏
#include <stdio.h>#include <math.h>int main(int argc, const char * argv[]) { int input; char c; int need=1; int count=3; scanf("%d %c",&input,&c); while...原创 2018-10-21 17:59:38 · 923 阅读 · 1 评论 -
素数对猜想
#include<stdio.h>#include<time.h>#include<math.h>#define N 100000int prime[100000];int a[N];int main(){ int i,j,len=0; int count=0; int input; scanf("%d",&...原创 2018-10-21 18:00:38 · 123 阅读 · 0 评论 -
6-1 Evaluate Postfix Expression (25 分)
6-1 Evaluate Postfix Expression (25 分)Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /.Format of functions:ElementType EvalPostf...原创 2018-10-23 18:45:44 · 3917 阅读 · 0 评论 -
习题2.3 数列求和-加强版 (20 分)
习题2.3 数列求和-加强版 (20 分)给定某数字A(1≤A≤9)以及非负整数N(0≤N≤100000),求数列之和S=A+AA+AAA+⋯+AA⋯A(N个A)。例如A=1, N=3时,S=1+11+111=123。输入格式:输入数字A与非负整数N。输出格式:输出其N项数列之和S的值。输入样例:1 3输出样例:123 #include <...原创 2018-10-24 15:15:13 · 4085 阅读 · 16 评论 -
7-1 树的同构 (25 分)
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。 图1图2现给定两棵树,请你判断它们是否是同构的。 输入格式:输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤...原创 2018-11-22 17:41:49 · 796 阅读 · 0 评论 -
7-2 List Leaves (25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.Input Specification:Each input file contains one test case. For each case, the first line gives...原创 2018-11-24 15:38:47 · 587 阅读 · 0 评论 -
6-9 Sort Three Distinct Keys (20 分)
Suppose you have an array of N elements, containing three distinct keys, "true", "false", and "maybe". Given an O(N) algorithm to rearrange the list so that all "false" elements precede "maybe" elemen...原创 2018-12-03 19:37:45 · 2156 阅读 · 0 评论 -
7-5 Tree Traversals Again (25 分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stac...原创 2019-04-29 20:10:50 · 819 阅读 · 0 评论