将0到9这十个数组成一位数加两位数加三位数等于四位数,0到9不能重复。用java如何写啊
2 回答
已采纳
呦呦米
TA贡献90条经验 获得超59个赞
public class Test { public static void main(String[] args) { int arr[] = new int[1000]; int a, b, c, d, e, f, k, i; int x, y, z, o, p, q, r, s, t; for (i = 123, k = 0; i <= 987; i++) { a = i % 10; b = i / 10 % 10; c = i / 100; if (a != b && b != c && a != c && b != 0 && a != 0) { //求得一个三位数的各个位数都不相同,并存到数组中 arr[k] = i; k++; } } int sum = 1; for (int m = 1; m <= 9; m++) //求得9! sum = sum * m; //System.out.println(sum); for (d = 0; d <= arr.length - 1 && arr[d] != 0; d++) { o = arr[d] % 10; p = arr[d] / 10 % 10; q = arr[d] / 100; for (e = 0; e <= arr.length - 1 && arr[e] != 0; e++) { r = arr[e] % 10; s = arr[e] / 10 % 10; t = arr[e] / 100; if(arr[d]==arr[e]) continue; //为减少循环的次数,当第一个数与第二个数相同时,继续下一层循环 for (f = 0; f <= arr.length - 1 && arr[f] != 0; f++) { x = arr[f] % 10; y = arr[f] / 10 % 10; z = arr[f] / 100; if(arr[f]==arr[e]||arr[f]==arr[d]) continue;//同上 if (((arr[d] + arr[e]) == arr[f]) && (arr[e] == 2 * arr[d]) && (o * p * q * s * r * t * x * y * z == sum)) System.out.println(arr[d] + " " + arr[e] + " " + arr[f]); } } } } } 运行结果: 192 384 576 219 438 657 273 546 819 327 654 981
添加回答
举报
0/150
提交
取消