吸血鬼数字的实现(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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值