求:5个不同数的和小于100的不重复组合的个数.
求效率比较高的算法。
多层for循环法首先排除,效率太差。
先谢谢大家了!
[该贴被python于2007-11-20 15:11修改过]
[该贴被python于2007-11-20 15:12修改过]
求效率比较高的算法。
多层for循环法首先排除,效率太差。
先谢谢大家了!
[该贴被python于2007-11-20 15:11修改过]
[该贴被python于2007-11-20 15:12修改过]
public static void main(String[] args) {
int a, b, c, d, e;
int tmp[4];
for(a=1; a<18; a++){
tmp[0] = (100 - a - 1 - (1+1) - (1+1+1)) / 4;
for(b =a+1; b<=tmp[0]; b++){
tmp[1] = (100 - a - b - 1 - (1+1)) / 3;
for (c=b+1; c<=tmp[1]; c++){
tmp[2] = (100 - a - b - c - 1) /2;
for(d=c+1; d<=tmp[2]; d++){
tmp[3] = (100 - a - b - c - d);
for(e=d+1; e<=tmp[3]; e++){
if (a+b+c+d+e<100){
System.out.println(a+" "+b+" "+c+" "+d+" "+e);
}else{
break;
}
}
}
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/
*
* @author root
*/
public class Main {
/
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a, b, c, d, e;
int[] tmp = new int[4];
for(a=1; a<18; a++){
tmp[0] = (100 - a - 1 - (1+1) - (1+1+1)) / 4;
for(b =a+1; b<=tmp[0]; b++){
tmp[1] = (100 - a - b - 1 - (1+1)) / 3;
for (c=b+1; c<=tmp[1]; c++){
tmp[2] = (100 - a - b - c - 1) /2;
for(d=c+1; d<=tmp[2]; d++){
tmp[3] = (100 - a - b - c - d);
for(e=d+1; e<=tmp[3]; e++){
if (a+b+c+d+e<100){
System.out.println(a+" "+b+" "+c+" "+d+" "+e);
}else{
break;
}
}
}
}
}
}
}
}