日期:2014-05-20 浏览次数:20936 次
import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * * @author cstur4 * */ public class CSDN { private int total; private int difficult; private Set<String> res = new HashSet<String>(); public CSDN(int total, int avg){ this.total = total; difficult = total*avg; } public Set<String> getComposition(){ int[] trace = new int[total+1]; getNum(difficult, 1, trace); return res; } public void getNum(int difficult, int th, int[] trace){ if(total == th){ if(difficult<=5){ trace[th] = difficult; Arrays.sort(trace); res.add(Arrays.toString(Arrays.copyOfRange(trace, 1, trace.length))); } return; } for(int i=1;i<=Math.min(5, difficult-(total-th));i++) { int[] trace1 = new int[total+1]; System.arraycopy(trace, 0, trace1, 0, trace.length); trace1[th] = i; getNum(difficult-i, th+1, trace1); } } public static void main(String[] args) { CSDN csdn = new CSDN(3, 3); Set<String> res = csdn.getComposition(); Iterator<String> it = res.iterator(); while(it.hasNext()) System.out.println(it.next()); } }