标签:imp n+1 inpu lan put [1] 代码 ack sample
1、满足F(n)=f(0)*f(n-1)+f(1)*f(n-2).....f(n-1)*f(0)的都是卡特兰数,另外一种表示是h(n)=c(2n,n)-c(2n,n+1)(n=0,1,2,...);这个公式还可以更简单得化为h(n)=C(2n,n)/(n+1);其前几项为 : 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796。
卡特兰数种经典题目:1、N个人有五元钱,N个人有十元钱,要买门票(五元每张)现在售票员没有零钱,有多少种买票的方法可以让售票员能不面临尴尬;
2、有n个数按1、2、3......n的顺序入栈,有多少种不同的出栈的方法;
3、n边形加把两点连接之后构成三角形,直线不能相交,有多少种加边的的方法;
卡特兰数的递推公式为:h(n)=h(n-1)*(4*n-2)/(n+1);
This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.
It‘s still a simple game, isn‘t it? But after you‘ve written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
InputEach line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.
OutputFor each n, print in a single line the number of ways to connect the 2n numbers into pairs.
Sample Input
2 3 -1
Sample Output
2 5
源代码:#include <iostream>
#include"cstring"
using namespace std;
const int maxn=300;
int main()
{
int N;
while(cin>>N)
{ if(N==-1)break;
int a[maxn][maxn],temp=0,len=1,sum1=0;
memset(a,0,sizeof(a));
a[0][0]=a[1][0]=1;
for(int i=2;i<=N;i++)
{ int sum=0;
for(int j=0;j<len;j<j++)
{
sum=a[i-1][j]*(4*i-2)+temp;
a[i][j]=sum%10;
temp=sum/10;
}
while(temp)
{
a[i][len++]=temp%10;
temp/=10;
}
int m=0;
for(int k=len-1;k>=0;k--)
{
m=sum1+a[i][k];
a[i][k]=m/(i+1);
sum1=(m%(i+1))*10;
}
for(int j=len-1;;j--)
if(a[i][j]==0)len--;
else break;
}
//for(;;j--)
// if(a[N][j])break;
for(int i=len-1;i>=0;i--)
cout<<a[N][i];
cout<<endl;
}
return 0;
}
标签:imp n+1 inpu lan put [1] 代码 ack sample
原文地址:http://www.cnblogs.com/qiaoqiaoa/p/7222447.html