//递归的方式:
private static String recursive(char[] chars, int pos) {
if(pos == 0) {
return "" + chars[pos];
}
String res = recursive(chars, pos - 1);
return "(" + res + ")*31 + " + chars[pos];
}
//在外面调用上面函数
String s = "abcdefg";
System.out.println(recursive(s.toCharArray(), s.toCharArray().length - 1)); |