L1-058 6翻了 (15 分)-PAT 团体程序设计天梯赛 GPLT

“666”是一种网络用语,大概是表示某人很厉害、我们很佩服的意思。最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思。如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字“27”,因为这是 3 个 “9”!

本题就请你编写程序,将那些过时的、只会用一连串“6666……6”表达仰慕的句子,翻译成最新的高级表达。

输入格式:
输入在一行中给出一句话,即一个非空字符串,由不超过 1000 个英文字母、数字和空格组成,以回车结束。

输出格式:
从左到右扫描输入的句子:如果句子中有超过 3 个连续的 6,则将这串连续的 6 替换成 9;但如果有超过 9 个连续的 6,则将这串连续的 6 替换成 27。其他内容不受影响,原样输出。

输入样例:
it is so 666 really 6666 what else can I say 6666666666

输出样例:
it is so 666 really 9 what else can I say 27

 

L1-057 PTA使我精神焕发 (5 分)-PAT 团体程序设计天梯赛 GPLT

以上是湖北经济学院同学的大作。本题就请你用汉语拼音输出这句话。

输入格式:
本题没有输入。

输出格式:
在一行中按照样例输出,以惊叹号结尾。

输入样例:

输出样例:
PTA shi3 wo3 jing1 shen2 huan4 fa1 ! 

[note]标点符号和数学符号所对应的英文

看英文文档时候会看到一些代码中标点符号的英文以前没有接触过,顺便就把键盘上所有的标点符号、常用的数学符号一起整理了一下~

` grave accent 重音符
~ tilde /‘tɪldə/ 波浪符号
swung dash 代字号,swing 摇摆
! exclamation point /ˌɛksklə’meʃən/ 感叹号
@ at 在…
# ① pound ②number ① 井号 ② …号
$ dollar 美元符号
% per cent 百分比
^ caret 插入符号
ellipsis points /ɪ’lɪpsɪs/ 省略号
& ampersand = and
* asterisk /‘æstərɪsk/ 星号
( ) ① parenthesis ② round brackets /pə’rɛnθəsɪs/ 圆括号,复数parentheses
① hyphen ② minus ① /‘haɪfn/ 连字号 ② 减号
—— dash 破折号
_ underline 下划线
+ plus 加号
= is equal to 等于号
[ ] square brackets 方括号
{ } curly brackets 花括号,大括号
\ backslash 反斜杠
| vertical bar,vertical virgule 竖线
|| parallel 双线号
; semicolon 分号
: colon 冒号
‘’ quotation mark 引号
“” double quotation mark 双引号

apostrophe /ə’pɑstrəfi/ 撇号
, comma 逗号
<> angle brackets 尖括号
< is less than 小于号
> is greater than 大于号
《》 French quotes 书名号;法文符号
. period,full stop,dot 句号
/ ① slash ② virgule ① 斜线 ② /‘vɝgjʊl/ 置于二字之间表示任取一字均可的短斜线
// ① slash-slash ② comment ① 双斜线 ② 注释符
? question mark 问号
since,because 因为
hence,therefore 因此,所以
square root 平方根
infinity /ɪn’fɪnəti/ 无穷号
Celsius system /‘selsiəs/ 摄氏度
perpendicular to 垂直于
intersection of 交集
union of 并集
summation of 总和
is equivalent to 全等于号
Is congruent to 全等号
is approximately equal to 约等于号
~ Is similar to 相似
± plus or minus 正负号
x is multiplied by 乘号
÷ is divided by 除号
is not equal to 不等于号
arrow 箭头
§ section,division 分节号
circumference 圆周
equals,as 等于,成比例
per mill 千分比
° degree
greater than or equal to 大于等于号
less than or equal to 小于等于号
division / fraction 分号
mod modulo 模运算符
Δ ① triangle ② Delta ① 三角符号 ② 第四个希腊字母Delta
much less than 远小于
much greater than 远大于
n! factorial 阶乘号

first-class type 一等类型的含义

一等(first-class)类型是指可以在执行期创造,并作为参数传递给其他函数或存入一个变数。

如果一个对象是一等类型,那么它:

  • 可以被存入变量或其他结构
  • 可以被作为参数传递给其他函数
  • 可以被作为函数的返回值
  • 可以在执行期创造,而无需完全在设计期全部写出
  • 即使没有被系结至某一名称,也可以存在

大部分语言的基本类型的数值(如int, float)等都是一等类型~

在C/C++中,函数不是一等类型,这表示函数在C/C++语言中不能在执行期创造,而必须在设计时全部写好,而在Python、Swift中函数是一等类型,这意味着函数可以作为其他函数的参数和返回值。

PAT 1155 Heap Paths (30 分)- 甲级

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))

One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.

Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.

Output Specification:
For each given tree, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.

Finally print in a line Max Heap if it is a max heap, or Min Heap for a min heap, or Not Heap if it is not a heap at all.

Sample Input 1:
8
98 72 86 60 65 12 23 50
Sample Output 1:
98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap
Sample Input 2:
8
8 38 25 58 52 82 70 60
Sample Output 2:
8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap
Sample Input 3:
8
10 28 15 12 34 9 8 56
Sample Output 3:
10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap

题目大意:给出一颗完全二叉树,打印出从根节点到所有叶节点的路径,打印顺序先右后左,即先序遍历的镜像。然后判断该树是大顶堆、小顶堆或者不是堆~

分析:1.深搜打印出所有路径(从右往左,即先序的镜像),vector保存一路上的节点,通过push和pop回溯,维护路径,index <= n是对只有左叶节点没有右叶节点的点特判
2.判断是否为堆:从第二个节点开始遍历,如果比父节点小,就不是小顶堆,如果比父节点大,就不是大顶堆~

 

在二叉树中有两个结点m和n,若m是n的祖先,则使用后序遍历可以找到从m到n的路径

首先需要理解的是,前中后序遍历都是通过递归的方式,将后来需要用到的结点保存在栈中,比如下面这颗树:

如果是前序遍历,根左右,过程是:根节点m入栈并输出,访问m的左孩子a,a入栈并输出,访问a的左孩子c,c入栈并输出,c没有左孩子,无元素入栈,c没有右孩子,无元素入栈,c出栈,此时栈顶元素为a,访问a元素的右孩子d,d入栈并输出,d没有左孩子,无元素入栈,d没有右孩子,无元素入栈,d出栈,a左右子树都访问完了所以出栈,现在栈顶元素是m,m已经没有作用了所以出栈,访问m的右孩子b,m的右孩子b入栈并输出,接着访问b的左孩子e,e入栈并输出……

如果中序遍历,左根右,过程是:根节点m入栈,访问m的左孩子,所以m的左孩子a入栈,访问a的左孩子,所以a的左孩子c入栈,c没有左孩子,c现在输出并出栈,c没有右孩子所以没有元素入栈,现在栈顶元素是a,a输出并出栈,a的右孩子d入栈,此时d没有左孩子所以没有元素入栈,d输出并出栈,d没有右孩子所以没有元素入栈,现在栈顶元素是m,m输出,此时m已经没有作用了所以出栈,m的右孩子b入栈,访问b的左孩子,所以b的左孩子e入栈……

所以在前序和中序的过程中,如果n在m的右子树部分,遍历过程中找到了n,但是m已经不在栈中,因为栈中只会保留等会需要用到的e和b结点,而m已经完成了访问根结点和m的左子树的任务,已经被出栈,所以无法追溯n如何走到m

但是后序遍历就不一样啦,后序的顺序是左右根,所以只要m的左右子树还没遍历完成,m就不能出栈,在遍历m的左右子树过程中,无论在m的左边还是右边找到了n,都可以直接返回然后根据栈中的路径让n找到回到m的路,这样就能找到m到n的路径~

所以一旦n在m的右子树,且离的较远,m就会在前序和中序的过程中因为已经完成了访问左、访问根的任务,而被栈遗忘,让找到了n的时候也不知怎么回到m……而只有后序会让作为祖先(子树的根)的m永远被铭记直到n找到m为止…