【NOIP1998 普及组】阶乘之和
题目很简单,但是数据的值很大,需要高精度,c++的话需要自己写一个高精度运算。
题目描述
用高精度计算出 S=1!+2!+3!+⋯+n!S=1!+2!+3!+⋯+n!(n≤50n≤50)。
其中“!”表示阶乘,例如:5!=5×4×3×2×15!=5×4×3×2×1。
输入格式
一个正整数 nn。
输出格式
一个正整数 SS,表示计算结果。
题目很简单,但是数据的值很大,需要高精度,c++的话需要自己写一个高精度运算。
用高精度计算出 S=1!+2!+3!+⋯+n!S=1!+2!+3!+⋯+n!(n≤50n≤50)。
其中“!”表示阶乘,例如:5!=5×4×3×2×15!=5×4×3×2×1。
一个正整数 nn。
一个正整数 SS,表示计算结果。
7-1 Is It An AVL Tree (25 point(s))
In computer science, an AVL tree (Georgy Adelson-Velsky and Evgenii Landis’ tree, named after the inventors) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one. (Quoted from wikipedia)
For each given binary search tree, you are supposed to tell if it is an AVL tree.
Each input file contains several test cases. The first line gives a positive integer K (≤10) which is the total number of cases. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary search tree. The second line gives the preorder traversal sequence of the tree with all the keys being distinct. All the numbers in a line are separated by a space.
For each test case, print in a line “Yes” if the given tree is an AVL tree, or “No” if not.
1 | 3 |
1 | Yes |
题目给出前序遍历的序列,可以利用这个序列一个一个插入来生成一棵树。之后再逐个计算节点因子判断是否为AVL树。
1 |
|
代码参考了https://www.cnblogs.com/littlepage/p/13194704.html
这代码写得挺妙的,每一步都有利用递归。
树结点的插入:
1 | Node* insert(Node *p, int value) |
利用函数的返回值来修改原值,避免了修改传入参数导致原值没有被修改。调用时记得要赋值Tree = insert(Tree, tmp);
判断结点:
1 | bool AVLJudge(Node *p) |
利用了尾递归将判断结果传回。
反转二叉树,虽然不是BST但也是树相关的题目,就也写一下吧。
The following is from Max Howell @twitter:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
今天做算法笔记上的题目又碰到了反转链表。题目不难,但是写起来就是感觉脑子有点绕,也可能只是我水平太差的原因。
这已经是我第三次碰到这道题了,第一次是大一下开学期末考的时候,当时在机房写了一个多小时,我自己测试都是对的,而且基本上考虑到了所有的情况,但是每次提交都有几个测试点通不过,甚至还换了一种方法重新写了一遍还是不过,当时真的感觉心态有点崩了。
考完了之后还是不服,把题目重新看了一下,才发现题目上说是带有头结点的链表。。。我真的是要脑溢血了,上课的时候老师还在强调不要用头结点,平时作业也没有用过头结点,考试的时候出一道带有头结点的题,我根本没注意到。不过这样也好,从此以后看题目就更加注重这种细节了。
Update your browser to view this website correctly.&npsb;Update my browser now