本题是计算卡特兰数,关于卡特兰数的详细说明请参考博客:https://blog.csdn.net/wookaikaiko/article/details/81105031
#include <iostream>
#include <cstring>
using namespace std;
const int LEN = 200;
int catalan[102][LEN];
int main()
{
memset(catalan, 0, sizeof(catalan));
catalan[1][LEN - 1] = 1; //卡特兰数的前两项catalan(0)、catalan(1)都为1
int n;
while (cin >> n)
{
//根据卡特兰数的通项公式 h(n)=h(n-1)*(4*n-2)/(n+1) 计算
int temp;
for (int i = 2; i <= n; i++)
{
int carryMul = 0;
for (int j = LEN - 1; j > 0; j--) //乘法部分
{
temp = catalan[i - 1][j] * (4 * i - 2) + carryMul;
carryMul = temp / 10;
catalan[i][j] = temp % 10;
}
int carryDiv = 0;
for (int j = 0; j < LEN; j++) //除法部分
{
temp = catalan[i][j] + carryDiv * 10;
carryDiv = temp % (i + 1);
catalan[i][j] = temp / (i + 1);
}
}
int index;
for (int i = 0; i < LEN; i++) //将有效位之前的0舍去
{
if (catalan[n][i] != 0)
{
index = i;
break;
}
}
for (int i = index; i < LEN; i++)
cout << catalan[n][i];
cout << endl;
}
return 0;
}
继续加油。