1. 程式人生 > >吸血鬼數字的實現(thinking in java練習題)

吸血鬼數字的實現(thinking in java練習題)

/**
 吸血鬼數字是指位數為偶數的數字,可以由一對數字相乘而得,而這對數字
 各包含乘積的一半位數的數字,其中從最初的數字中選取的數字可以任意排序。
 以兩個0結尾的數字是不允許的。
 例如,下面數字都是吸血鬼數字:
 1260 = 21 * 60
 1827 = 21 * 87
 2187 = 27 * 81
 寫一個程式,找出4位數的所有吸血鬼數字。
**/
public class Test_05{
	public static void main(String[] args){
		check();
	}
	public static void check(){
		for(int i = 1000; i < 10000; i++){
			if(i %100 == 0) continue;
			int o = i%10;			//one
			int t = i/10%10;		//ten
			int h = i/100%10;	//hundrod
			int th = i/1000;	//thound
			//System.out.println("i = " + i + " = " + thound + hundrod + ten + one);
			if(i == (th * 10 + h) * (t * 10 + o)){
				System.out.println(i + " = " + th + h + " * " + t + o);
			}
			if(th != h && i == (h * 10 + th) * (t * 10 + o)){
				System.out.println(i + " = " + h + th + " * " + t + o);
			}
			if(o != t && i == (th * 10 + h) * (o * 10 + t)){
				System.out.println(i + " = " + th + h + " * " + o + t);
			}
			if(th != h && o != t && i == (h * 10 + th) * (o * 10 + t)){
				System.out.println(i + " = " + h + th + " * " + o + t);
			}
			
			if(t != h){
				if(i == (th * 10 + t) * (h * 10 + o)){
					System.out.println(i + " = " + th + t + " * " + h + o);
				}
				if(th != t && i == (t * 10 + th) * (h * 10 + o)){
					System.out.println(i + " = " + t + th + " * " + h + o);
				}
				if(o != h && i == (th * 10 + t) * (o * 10 + h)){
					System.out.println(i + " = " + th + t + " * " + o + h);
				}
				if(th != t && o != h && i == (t * 10 + th) * (o * 10 + h)){
					System.out.println(i + " = " + t + th + " * " + o + h);
				}
			}
			
			if(o != h){
				if(i == (th * 10 + o) * (h * 10 + t)){
					System.out.println(i + " = " + th + o + " * " + h + t);
				}
				if(th != o && i == (o * 10 + th) * (h * 10 + t)){
					System.out.println(i + " = " + o + th + " * " + h + t);
				}
				if(h != t && i == (th * 10 + o) * (t * 10 + h)){
					System.out.println(i + " = " + th + o + " * " + t + h);
				}
				if(th != o && h != t && i == (o * 10 + th) * (t * 10 + h)){
					System.out.println(i + " = " + o + th + " * " + t + h);
				}
			}
		}
	}
}
// 1234: 12 * 34 || 21 * 34 || 12 * 43 || 21 * 43
// 1234: 13 * 24 || 31 * 24 || 13 * 42 || 31 * 42
// 1234: 14 * 23 || 41 * 23 || 14 * 32 || 41 * 32