The questions are as follows:
Parentheses matching
Input n parentheses and output n parentheses that can be combined. For example --
Only () is input;
When the input is 2, there are two types: () and;
When 3 is input, there are (), () and ((())), five types.
There are 14 input values .. And so on.
I think of stupid methods, and so on, because I believe there must be a formula. After a while. I did not summarize it, so I read the book because I have read similar questions in my impressions.
Then the formula is found. Haha.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.close(); if (n == 0) { System.out.println(0);}else{ System.out.println((int) (jiecheng(2 * n) / (jiecheng(n) * jiecheng(n) * (n + 1)))); } } private static long jiecheng(int n) { long result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } }