public static void main(String[] args) {
List<Integer> list = new ArrayList<>();//记录每组数的值 每组数a<=b<=c<=d
List<Integer> rlist = new ArrayList<>();//记录最后的结果 for(int i = 1; i <= 10 ; i++) {
for(int j = i; j <= 13 ; j++) {
for(int k = j; k <= 19 ; k++) {
list.add(i);
list.add(j);
list.add(k);
list.add(40-i-j-k);
}
}
} int a,b,c,d;
int len = list.size()/4;
for (int i = 0; i < len; i++) {
a = list.get(4*i);
b = list.get(4*i+1);
c = list.get(4*i+2);
d = list.get(4*i+3);
if(result(a, b, c, d)) {
rlist.add(a);
rlist.add(b);
rlist.add(c);
rlist.add(d);
}
}
len = rlist.size()/4;
for(int i = 0; i < len; i++) {
System.out.println("40 = "+rlist.get(4*i)+" + "+rlist.get(4*i+1)+" + "+rlist.get(4*i+2)+" + "+rlist.get(4*i+3));
}
} public static boolean result(int a,int b,int c,int d){
TreeSet<Integer> set = new TreeSet<>();
int[] arr = {-1,0,1};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
for (int k = 0; k < arr.length; k++) {
for (int l = 0; l < arr.length; l++) {
int sum = a*arr[i]+b*arr[j]+c*arr[k]+d*arr[l]; if(sum > 0) set.add(sum);
}
}
}
}
if(set.size() == 40 && set.first() == 1)
return true;
else
return false;
}
05-06 08:11