四位吸血鬼数算法
题目来自《Thinking in Java》第四版,第四章最后一道练习题。
下面是我的代码:
public static void main(String args[]) {
long t1 = System.currentTimeMillis();
int number = 0;
int[] combo = new int[4];
left:
for(int left=11;left<100;left++) {
right:
for(int right=11;right<=left;right++) {
number = left * right;
if(number < 1000 || (number-left-right) % 9 != 0 || number % 100 == 0) {
continue right;
}
combo[0] = left / 10;
combo[1] = left % 10;
combo[2] = right / 10;
combo[3] = right % 10;
A:
for(int a=0;a<=3;a++) {
B:
for(int b=0;b<=3;b++) {
if(a == b) {
continue;
}
C:
for(int c=0;c<=3;c++) {
if(c == a || c == b) {
continue;
}
D:
for(int d=0;d<=3;d++) {
if(d == a || d == b || d == c) {
continue;
}
int n = combo[a] * 1000 + combo[b] * 100 + combo[c] * 10 + combo[d];
if(n == number) {
out(n + "=" + left + "*" + right);//这里自己替换成System.out.println()
continue right;
}
continue C;
}
}
}
}
}
}
long t2 = System.currentTimeMillis();
out("耗时:" + (t2-t1));//这里自己替换成System.out.println()
}
控制台输出结果: