找出四位数的吸血鬼数字

四位数的吸血鬼数字,例如:1260=21*60 1395=93*15

package 吸血鬼数字;

public class XiXueGui {

	public static void main(String[] args) {
		for(int num = 1001;num < 10000;num++) {
			math(num);
		}

	}
	public static void math(int num) {
		int [] temp1 = new int[2];
		int [] temp2 = new int[2];
		int a = num / 1000;
		int b = num / 100 % 10;
		int c = num / 10 % 10;
		int d = num % 10;
		int[] data = {a,b,c,d};
		for(int i = 0;i<data.length;i++) {
			for(int j = 0;j < data.length;j++) {
				if(i==j) {
					continue;
				}
				temp1[0] = data[i];
				temp1[1] = data[j];
				for(int m = 0;m<data.length;m++) {
					if(m != i && m != j) {
						temp2[0] = data[m];
						for(int n = 0;n<data.length;n++) {
							if(n != i && n != j && n != m) {
								temp2[1] = data[n];
								multi(data,temp1,temp2);
							}
						}
					}
				}
			}
		}
}
public static int toInt(int[] temp) {
	int m = 0;
	int [] temp1 = new int[temp.length];
	for(int i = 0;i < temp.length;i++) {
		temp1[i] = temp[i]*(int)Math.pow(10,temp.length-1-i);
	}
	for(int i = 0;i < temp1.length;i++) {
		m+=temp1[i];
	}
	return m;
}
public static void multi(int[] temp,int[] temp1,int[] temp2) {
	int i = toInt(temp1);
	int j = toInt(temp2);
	int k = toInt(temp);
	if(k == i*j) {
		System.out.println(k + "=" + i + "*" + j);
	}
 }

}

输出结果:

1260=21*60
1260=60*21
1395=15*93
1395=93*15
1435=41*35
1435=35*41
1530=51*30
1530=30*51
1827=87*21
1827=21*87
2187=27*81
2187=81*27
6880=86*80
6880=80*86
6880=86*80
6880=80*86

猜你喜欢

转载自blog.csdn.net/sinat_40959511/article/details/80301609