# Multiple power problems and output each case java

Keywords: Java

## 1, Problem description

1. Set n variables x1,x2 xn. By using these variables as the base and the powers of each layer in order, we can get the N-power as follows
2. In this paper, the above N-power is regarded as indeterminate, and it can become a definite N-power only after proper parentheses are added. Different bracketed ways lead to different n-powers. For example, when n=4, all four powers have five.
3. How many different powers of N are calculated for n variables.
4. Output n power in each case

## 2, Problem analysis and algorithm analysis

1. Using the idea of divide and conquer, the original problem is regarded as a sequence problem, the sequence order is invariable, adding proper brackets to the sequence to determine an n-power.
2. According to the idea of divide and conquer, the original problem is decomposed into atomic problem, that is, to find a power first.
3. Find the nth power i n the form of a[i] = "(" + a[j]+a[i-j] + ")". a[j] and a[i-j] are n cases in which the nth or I-j power exists in a set.
Traversing all cases of a[n] output N-power, a[n].size() is the number of cases of N-power.

## Code

```import java.util.ArrayList;

/**
* Describe multiple powers
* @author THTF
* @date 2018 July 9th 2013
*/

public class MultiplePower {

private static StrList[] l; //The subscript represents the nth power. str is all forms of ArrayList array used to store the nth power

public MultiplePower(int n) {
l = new StrList[n + 1];     //Initialization
for (int i = 0; i < n + 1; i++) {
l[i] = new StrList();   //Initialization
}
l.str.add(null); //Subscript 0 is not used
l.str.add("*");      //No parentheses when 1 power
for (int i = 2; i <= n; i++) {
for (int j = 1; j < i; j++) {
for (String str2 : l[j].str) {
for (String str3 : l[i - j].str) {
l[i].str.add("(" + str2 + str3 + ")");
}
}
}
}
show(n);
}

/**
* Describe all n powers of output
* @param n
*/
public static void show(int n) {
for (String i : l[n].str) {
StringBuilder sb = new StringBuilder(i);
int counter = 0;
for (int k = 0; k < i.length(); k++) {
if (sb.charAt(k) == '*') {
sb.setCharAt(k, (char) ('A' + counter++));
}
}
System.out.println(sb);
System.out.println("common"+l[n].str.size()+"species");
}
}
}

/**
*
* @author THTF
* @date 2018 July 9th 2013
*/
class StrList {

public ArrayList<String> str;

public StrList() {
str = new ArrayList<String>(10000);

}
}```

Posted by tyty1993 on Fri, 31 Jan 2020 03:40:40 -0800