早教吧作业答案频道 -->其他-->
急寻高手解释一个程序!题目是PKU2775DescriptionManypeopleknowsbinarysearchtree.ThekeysinabinarysearchtreearealwaysstoredinsuchawayastosatisfytheBSTproperty:LetXbeanodeinabinarysearchtree.IfYis
题目详情
急寻高手解释一个程序!
题目是PKU 2775
【Description】
Many people knows binary search tree. The keys in a binary search tree are always stored in such a way as to satisfy the BST property: Let X be a node in a binary search tree. If Y is a node in the left subtree of X , then Y X. For example,
It is a binary search tree. And it can be built by inserting the elements of vector A= (12, 6, 3, 18, 20, 10, 4, 17, 20) sequentially. But it can also be built by the vector B= (12, 18, 17, 6, 20, 3, 10, 4, 20). Now given a vector X, then you may get a binary search tree from X. Your job is to calculate how many different vectors can build the same binary search tree. To make it easy, you should just output the number of different vectors mod 9901.
【Input】
Input consists of several cases. Each case starts with a line containing one positive integer n, which is the length of test vector. The integer n is less than 20. Following this there will be n positive integers, which are less then 1000, on the next line. The input will end with a case starting with n = 0. This case should not be processed.
【Output】
For each test case, print a line with a single integer, which is the number of different vectors mod 9901.
【Sample Input】
3
2 1 3
9
5 6 3 18 20 10 4 17 20
0
【Sample output】
2
168
下面是我在网上找到的一个代码,本人是菜鸟,程序没有注释,看不懂,哪位高手能够帮忙给程序添加一些详细的注释,万分感谢,请先说一下这个题目的思路!
#include
#include
const int max=20025;
int tree[max],size[max];
int da[10001];
int an;
void insert(int x)
{
int p,y;
p=1;y=1;
//查找
while(size[p]!=0)
{
size[p]++;y=p;
if(x
题目是PKU 2775
【Description】
Many people knows binary search tree. The keys in a binary search tree are always stored in such a way as to satisfy the BST property: Let X be a node in a binary search tree. If Y is a node in the left subtree of X , then Y X. For example,
It is a binary search tree. And it can be built by inserting the elements of vector A= (12, 6, 3, 18, 20, 10, 4, 17, 20) sequentially. But it can also be built by the vector B= (12, 18, 17, 6, 20, 3, 10, 4, 20). Now given a vector X, then you may get a binary search tree from X. Your job is to calculate how many different vectors can build the same binary search tree. To make it easy, you should just output the number of different vectors mod 9901.
【Input】
Input consists of several cases. Each case starts with a line containing one positive integer n, which is the length of test vector. The integer n is less than 20. Following this there will be n positive integers, which are less then 1000, on the next line. The input will end with a case starting with n = 0. This case should not be processed.
【Output】
For each test case, print a line with a single integer, which is the number of different vectors mod 9901.
【Sample Input】
3
2 1 3
9
5 6 3 18 20 10 4 17 20
0
【Sample output】
2
168
下面是我在网上找到的一个代码,本人是菜鸟,程序没有注释,看不懂,哪位高手能够帮忙给程序添加一些详细的注释,万分感谢,请先说一下这个题目的思路!
#include
#include
const int max=20025;
int tree[max],size[max];
int da[10001];
int an;
void insert(int x)
{
int p,y;
p=1;y=1;
//查找
while(size[p]!=0)
{
size[p]++;y=p;
if(x
▼优质解答
答案和解析
这是个二叉树搜索算法,分为左子树和右子树进行分叉搜索.
关键函数:void lookup(int p)
关键句:
if(size[2*p+1]+size[2*p]==0)return;
if(size[2*p]) lookup(2*p);
if(size[2*p+1]) lookup(2*p+1);
主程序中先将输入的数据构造成树,然后进行搜索,invx是个计算符合条件数据的函数.
不好意思,这东西我自己看了也想吐,就帮你这么多吧,我得去呕了.
关键函数:void lookup(int p)
关键句:
if(size[2*p+1]+size[2*p]==0)return;
if(size[2*p]) lookup(2*p);
if(size[2*p+1]) lookup(2*p+1);
主程序中先将输入的数据构造成树,然后进行搜索,invx是个计算符合条件数据的函数.
不好意思,这东西我自己看了也想吐,就帮你这么多吧,我得去呕了.
看了 急寻高手解释一个程序!题目是...的网友还看了以下:
1.已知C(7,n+1)-C(7,n)=C(8,n),那么n的值是?2.下列四个式子的值与A(m, 2020-05-14 …
1.已知C(7,n+1)-C(7,n)=C(8,n),那么n的值是?2.下列四个式子的值与A(m, 2020-05-14 …
根据里氏震级的定义,地震所释放的相对能量E与地震级数n的关系为:E=10n,那么9级地震所释放的相 2020-06-07 …
C(n,k)=C(n-1,k-1)+C(n-1,k)为什么这个等式成立?请大神帮我解释下C(n,k 2020-06-12 …
对数列{an}和{bn},若对任意正整数n,恒有bn≤an,则称数列{bn}是数列{an}的“下界 2020-07-31 …
对数列{an}和{bn},若对任意正整数n,恒有bn≤an,则称数列{bn}是数列{an}的“下界 2020-07-31 …
求证关于n进制的问题(2个,自己发现的)1.对于任意n进制正整数m,进行如下操作:把所有数字加起来如 2021-02-04 …
根据里氏震级的定义,地震所释放的相对能量E与地震级数n的关系为:E=10n,那么9级地震所释放的相对 2021-02-05 …
根据里氏震级的定义,地震所释放的相对能量E与震级n的关系为E=10n,那么9级地震所释放的相对能量是 2021-02-05 …
(2011安徽,12,5分)根据里氏震级的定义,地震所释放的相对能量E与震级n的关系为E=10n,那 2021-02-05 …