整数的划分问题,要求将所有可能性输出,用Java或c++都可以
1个回答

import java.util.HashMap;

import java.util.Map;

public class Test {

private static void getString(String t, int h, int o, Map map) {

if (h > o) {

getString(t, o, o, map);

} else {

if (h < o) {

getString(h + "+" + t, h, o - h, map);

for (int i = h - 1; i >= 2; i--) {

getString(h + "+" + t, i, o - h, map);

}

} else {

String out = h + "+" + t;

out = out.substring(0, out.length() - 1);

map.put(out, out);

for (int i = h - 1; i >= 2; i--) {

getString(t, i, o, map);

}

}

String out = t + "";

for (int x = 0; x < o; x++)

out = 1 + "+" + out;

out = out.substring(0, out.length() - 1);

map.put(out, out);

}

}

public static void outAll(int n) {

Map map = new HashMap();

getString("", n, n, map);

for (String key : map.keySet()) {

System.out.println(key);

}

}

public static void main(String[] args) {

outAll(6);

}

}