PTA作业题集
学数据结构算法的做过的实验题、编程题。
基本纯c数据结构实现,部分题目写上了比较完备的思路。希望对初学数据结构的同学有所帮助。
Reza.
宾州州立大学博士在读,主要做点NLP。
个人主页:https://renzelou.github.io/
展开
-
1001 A+B Format (20分)
Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).Input Specification:Each input fil...原创 2020-02-02 10:35:14 · 160 阅读 · 0 评论 -
1020 Tree Traversals (25分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the cor...原创 2020-01-28 11:39:34 · 216 阅读 · 0 评论 -
1012 The Best Rank (25分)
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - Eng...原创 2020-02-04 13:12:43 · 148 阅读 · 0 评论 -
1010 Radix (25分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.Now for any pair of positive int...原创 2020-02-04 14:51:36 · 181 阅读 · 0 评论 -
1005 Spell It Right (20分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.Input Specification:Each input file contains one test case. Each ...原创 2020-02-03 11:45:38 · 173 阅读 · 0 评论 -
1002 A+B for Polynomials (25分)
This time, you are supposed to find A+B where A and B are two polynomials.InputEach input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynom...原创 2020-02-02 11:22:32 · 150 阅读 · 0 评论 -
1050 String Subtraction (20分)
Given two strings S1 and S2, S=S1−S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1−S2 for any given s...原创 2020-01-31 11:10:47 · 221 阅读 · 0 评论 -
1015 Reversible Primes (20分)
A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a pr...原创 2020-02-05 10:49:10 · 208 阅读 · 0 评论 -
1051 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 possible pop sequence of the...原创 2020-01-27 15:07:49 · 215 阅读 · 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...原创 2020-01-14 23:56:20 · 384 阅读 · 0 评论 -
1092 To Buy or Not to Buy (20分)
Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sel...原创 2020-01-22 16:00:59 · 129 阅读 · 0 评论 -
7-9 词频统计 (30 分)C语言实现
请编写程序,对一段英文文本,统计其中所有不同单词的个数,以及词频最大的前10%的单词。所谓“单词”,是指由不超过80个单词字符组成的连续字符串,但长度超过15的单词将只截取保留前15个单词字符。而合法的“单词字符”为大小写字母、数字和下划线,其它字符均认为是单词分隔符。输入格式:输入给出一段非空文本,最后以符号#结尾。输入保证存在至少10个不同的单词。输出格式:在第一行中输出文本中所有不...原创 2019-10-20 15:26:11 · 2250 阅读 · 1 评论 -
1009 Product of Polynomials (25分)
This time, you are supposed to find A×B where A and B are two polynomials.Input Specification:Each input file contains one test case. Each case occupies 2 lines, and each line contains the informati...原创 2020-02-04 10:52:18 · 149 阅读 · 0 评论 -
7-9 部落 (25分)
输入样例:43 10 1 22 3 44 1 5 7 83 9 6 4210 53 7输出样例:10 2YN最基本的并查集,带权路径压缩源码:#include<stdio.h>#define Max 10010 int find(int x,int* S){ if(S[x]<0)return x; else return S[x]=...原创 2020-01-14 22:56:34 · 913 阅读 · 0 评论 -
1090 Highest Price in Supply Chain (25分)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on the...原创 2020-02-01 11:22:56 · 159 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node’s key.The right subt...原创 2020-01-24 12:33:06 · 150 阅读 · 0 评论 -
1008 Elevator (20分)
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 sec...原创 2020-01-26 12:01:22 · 172 阅读 · 1 评论 -
1058 A+B in Hogwarts (20分)
If you are a fan of Harry Potter, you would know the world of magic has its own currency system – as Hagrid explained it to Harry, “Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sic...原创 2020-01-31 11:33:04 · 148 阅读 · 0 评论 -
7-7 六度空间 (30分)
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太...原创 2020-01-15 00:15:41 · 417 阅读 · 2 评论 -
7-12 How Long Does It Take (25分)
Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.Input Specification:Each input file contains one test case. Each case sta...原创 2020-01-15 00:00:38 · 265 阅读 · 0 评论 -
1079 Total Sales of Supply Chain (25分)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on the...原创 2020-02-01 11:35:36 · 207 阅读 · 0 评论 -
7-18 插入排序还是归并排序 (25分)
根据维基百科的定义:插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列。每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置。如此迭代直到全部元素有序。归并排序进行如下迭代操作:首先将原始序列看成 N 个只包含 1 个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下 1 个有序的序列。现给定原始序列和由某排序算法产生的中间序列,请你判断该算法究...原创 2020-01-14 23:39:02 · 1534 阅读 · 0 评论 -
7-20 子集和问题 (25分)
设集合S={x1,x2,…,xn}是一个正整数集合,c是一个正整数,子集和问题判定是否存在S的一个子集S1,使S1中的元素之和为c。试设计一个解子集和问题的回溯法。输入格式:输入数据第1行有2个正整数n和c,n表示S的大小,c是子集和的目标值。接下来的1行中,有n个正整数,表示集合S中的元素。 是子集和的目标值。接下来的1 行中,有n个正整数,表示集合S中的元素。输出格式:输出子集和问题的...原创 2020-01-14 23:42:35 · 3496 阅读 · 0 评论 -
1007 Maximum Subsequence Sum (25分)
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence w...原创 2020-02-03 12:39:15 · 312 阅读 · 0 评论 -
7-6 列出连通集 (25分)
输入样例:8 60 70 12 04 12 43 5输出样例:{ 0 1 4 2 7 }{ 3 5 }{ 6 }{ 0 1 2 7 4 }{ 3 5 }{ 6 }源码:#include<stdio.h>#include<stdlib.h>#include<string.h>#define Max 10typede...原创 2020-01-15 00:11:34 · 359 阅读 · 0 评论 -
1027 Colors in Mars (20分)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 dig...原创 2020-01-27 10:52:34 · 114 阅读 · 0 评论 -
7-19 月饼 (25分)
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 1...原创 2020-01-14 23:40:32 · 340 阅读 · 0 评论 -
7-9 旅游规划 (25分)
有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。输入格式:输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速公路的条数;S是出发地的城市编号;...原创 2020-01-15 00:18:37 · 274 阅读 · 0 评论 -
7-22 最长公共前后缀 (25分)
字符串的前缀是指不包含最后一个字符的所有以第一个字符开头的连续子串;后缀是指不包含第一个字符的所有以最后一个字符结尾的连续子串。例如对于字符串 abacaba,其前缀有 a, ab, aba, abac, abacab,后缀有bacaba, acaba, caba, aba, ba, a。最长公共前后缀就是 aba。现给出一个长度为 N 的字符串 S,对于每个 M (0<=M<N),求...原创 2020-01-14 23:45:31 · 621 阅读 · 0 评论 -
1086 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...原创 2020-01-28 12:50:30 · 216 阅读 · 0 评论 -
1056 Mice and Rice (25分)
Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice...原创 2020-01-27 13:29:00 · 172 阅读 · 0 评论 -
1096 Consecutive Factors (20分)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given ...原创 2020-01-24 13:11:31 · 102 阅读 · 0 评论 -
1006 Sign In and Sign Out (25分)
At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you...原创 2020-01-29 10:15:18 · 127 阅读 · 0 评论 -
7-14 畅通工程之局部最小花费问题 (35分)
某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出“畅通工程”的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建快速路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全地区畅通需要的最低成本。输入格式:输入的第一行给出村庄数目N (1≤N≤100)...原创 2020-01-14 23:27:18 · 672 阅读 · 0 评论 -
1094 The Largest Generation (25分)
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.Input S...原创 2020-01-22 14:23:06 · 163 阅读 · 0 评论 -
7-4 顶点的度 (20 分)c语言实现
顶点的图。给定一个有向图,输出各顶点的出度和入度。输入格式:输入文件中包含多个测试数据,每个测试数据描述了一个无权有向图。每个测试数据的第一行为两个正整数n 和m,1 ≤ n ≤ 100,1 ≤ m ≤ 500,分别表示该有向图的顶点数目和边数,顶点的序号从1 开始计起。接下来有m 行,每行为两个正整数,用空格隔开,分别表示一条边的起点和终点。每条边出现一次且仅一次,图中不存在自身环和重边。输...原创 2019-10-20 15:49:31 · 1725 阅读 · 0 评论 -
1042 Shuffling Machine (20分)
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid “inside jobs” where employees collaborate with gambler...原创 2020-01-30 10:58:10 · 145 阅读 · 0 评论 -
1054 The Dominant Color (20分)
Behind the scenes in the computer’s memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the d...原创 2020-01-26 12:27:34 · 170 阅读 · 0 评论 -
6-14 Count Connected Components (20分)
Write a function to count the number of connected components in a given graph.Format of functions:int CountConnectedComponents( LGraph Graph );where LGraph is defined as the following:typedef str...原创 2020-01-14 23:54:31 · 551 阅读 · 0 评论 -
1041 Be Unique (20分)
Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1,10^4]. The first one who bets on...原创 2020-01-30 10:23:26 · 203 阅读 · 0 评论