标签:
How Many Trees?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3232 Accepted Submission(s): 1878
Problem Description
A binary search tree is a binary tree with root k such that any node v reachable from its left has label (v) <label (k) and any node w reachable from its right has label (w) > label (k). It is a search structure which can find a node
with label x in O(n log n) average time, where n is the size of the tree (number of vertices).
Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?
Input
The input will contain a number 1 <= i <= 100 per line representing the number of elements of the set.
Output
You have to print a line in the output for each entry with the answer to the previous question.
Sample Input
Sample Output
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[110][110];
int b[110];
void catalan()
{
int i,j,len,carry,temp;
a[1][0]=b[1]=1;
len=1;
for(i=2;i<=100;i++)
{
for(j=0;j<len;j++)
{
a[i][j]=a[i-1][j]*(4*(i-1)+2);
}
carry=0;
for(j=0;j<len;j++)
{
temp=a[i][j]+carry;
a[i][j]=temp%10;
carry=temp/10;
}
while(carry)
{
a[i][len++]=carry%10;
carry/=10;
}
carry=0;
for(j=len-1;j>=0;j--)
{
temp=carry*10+a[i][j];
a[i][j]=temp/(i+1);
carry=temp%(i+1);
}
while(!a[i][len-1])
{
len--;
}
b[i]=len;
}
}
int main()
{
int i,n;
catalan();
while(scanf("%d",&n)!=EOF)
{
for(i=b[n]-1;i>=0;i--)
{
printf("%d",a[n][i]);
}
printf("\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdoj 1130 How Many Trees? 【卡特兰数】
标签:
原文地址:http://blog.csdn.net/longge33445/article/details/48024205