How many sequence problems are there for an outbound stack. It is generally known that it is the catalan number.
The problem is that the number of Catalan is large, so high-precision computing is required.
In addition, table creation is much faster. Note the following formula:
Catalan number formula Cn = C (2n, N)/(n + 1 );
Recursive Formula C (n) = C (n-1) * (4 * N-2)/(n + 1)
The knowledge of high precision multiplied by an integer and high precision divided by an integer. In this way, it is better to use an integer array for computation. If you use a string, it is not easy to calculate it, because the integer may also be multiple digits.
Const int max_n = 101; short Catalan [max_n] [max_n]; // catlans [I] [0] Save the lengthvoid calcatalans () {short carry = 0, Len = 1; catalan [1] [0] = 1; Catalan [1] [1] = 1; Catalan [2] [0] = 1; Catalan [2] [1] = 2; for (INT I = 3; I <max_n; I ++) {carry = 0; For (Int J = 1; j <= Len; j ++) // multiply the precision by an integer {short sum = Catalan [I-1] [J] * (I <2)-2) + carry; carry = sum/10; catalan [I] [J] = sum % 10;} while (carry) {Catalan [I] [++ L En] = carry % 10; carry/= 10;} For (Int J = Len; j> 0; j --) // divide the precision by an integer {short sum = Catalan [I] [J] + carry * 10; Catalan [I] [J] = sum/(I + 1 ); carry = sum % (I + 1); // do not need to consider the remainder} while (Catalan [I] [Len] = 0) Len --; catalan [I] [0] = Len ;}} int main () {calcatalans (); int N; while (~ Scanf ("% d", & N) {for (INT I = Catalan [N] [0]; I> 0; I --) {printf ("% d ", catalan [N] [I]);} putchar ('\ n');} return 0 ;}