[0, 0, 1, 0, 2, 0, 1, 2]
[0, 0, 1, 2, 1, 0, 2, 0]
[0, 0, 2, 0, 2, 0, 1, 0]
[0, 0, 2, 2, 2, 2, 1, 2]
[0, 1, 1, 1, 2, 2, 1, 0]
[0, 1, 2, 1, 1, 0, 1, 1]
[0, 2, 2, 1, 2, 1, 1, 0]
[1, 0, 2, 1, 0, 1, 1, 1]
[1, 0, 2, 1, 1, 1, 0, 2]
[1, 1, 0, 2, 1, 0, 2, 1]
[1, 1, 2, 1, 1, 1, 0, 1]
0表示不插入,1表示插入+,2表示插入-
代码实在是太乱。。
import java.util.*;
class Test
{
public static void main(String[] args){
//0表示什么都不插入
//1表示插入+
//2表示插入-
int[] arr = {0,0,0,0,0,0,0,0};
int sum = sum(arr);
System.out.println("sum="+sum);
while (arr[0]!=3)
{
if (sum(arr)==100){
System.out.println(Arrays.toString(arr));
}
//遍历所有可能
arr[7]++;
for (int x = 7;x>0 ;x-- )
{
if (arr[x]==3)
{
arr[x]=0;
arr[x-1]++;
}
}
}
}
//传入符号顺序,返回结果
public static int sum(int[] arr){
int[] a = {1,2,3,4,5,6,7,8,9};
int sum = 0;
int temp = 9;
int num = 10;
for(int x=arr.length-1;x>=0;x--){
if(arr[x]==0){
temp += a[x]*num;
num *= 10;
}
else if (arr[x]==1){
num = 10;
sum += temp;
temp = a[x];
}
else{
num = 10;
sum -= temp;
temp = a[x];
}
}
sum += temp;
return sum;
}
} |