一个数组有11个整数分别为1 4 1048576 16 32 64 128 8192 2097152 16777216写一段java代码列出数组中整数组合相加后的所有结果数组中的每个整数只能使用一次
public class ArraySum {
public static void main(String[] args) {
int[] arr = {1, 4, 1048576, 16, 32, 64, 128, 8192, 2097152, 16777216};
int n = arr.length;
int sum = 0;
for(int i = 0; i < (1 << n); i++) {
int temp = 0;
for(int j = 0; j < n; j++) {
if((i & (1 << j)) > 0) {
temp += arr[j];
}
}
sum += temp;
}
System.out.println("所有整数组合相加的结果为:" + sum);
}
}
原文地址: https://www.cveoy.top/t/topic/b2u0 著作权归作者所有。请勿转载和采集!