#include
#define N 20
void fun(int arr[][N],int r, int c, int n){
int i, d;
if(r==0 && c==0){
arr[r][c]=1;
fun(arr,r+1,0,n);
}else if(c==0){
arr[r][c]=1;
fun(arr,r,c+1,n);
}else if(r==c){
d=0;
for(i=0; i<=r-1; i++)
d+=arr[r-1][i];
arr[r][c]=d;
if(r==n)
return;
else
fun(arr,r+1,0,n);
}else{
arr[r][c]=arr[r][c-1]+arr[r-1][c];
fun(arr,r,c+1,n);
}
}
int main(){
int a[N][N];
int i, j ,n;
printf("n(<20)=?");
scanf("%d",&n);
fun(a,0,0,n-1);
for(i=0; i
printf("%6d",a[i][j]);
printf("\n");
}
return 0;
}
#include
void Catalan(long long *arr1,long long *arr2 ,int n)
{
if (n > 1) Catalan(arr2, arr1, n-1);
*(arr1+1) = *(arr2+1) = 1;
for (int i=1; i <= n+1; i++)
{
*(arr2+i) = *(arr1+i) + *(arr2+i-1);
if ( i <= n) std::cout << *(arr1 + i) << "\t" ;
}
std::cout << std::endl;
}
int main()
{
long long a[50]={0},b[50]={0};
long long *arr1 = &a[0];
long long *arr2 = &b[0];
int n;
std::cout << "please enter a number: ";
while ( !(std::cin >>源坦 n) )
{
std::cin.clear();
while (std::cin.get() != '\n'塌槐 ) { continue; }
std::cout << "雹衫桐bad input! please enter number: ";
}
Catalan(arr1,arr2,n);
return 0;
}