java编程找出吸血鬼数字,找出四位數的所有吸血鬼數字(JAVA)

/**

* 找出四位數的所有吸血鬼數字

* 吸血鬼數字是指位數為偶數的數字,可以由一對數字相乘而得到,而這對數字各包含乘積的一半位數的數字,其中從最初的數字中選取的數字可以任意排序.

* 以兩個0結尾的數字是不允許的。

* 例如下列數字都是吸血鬼數字

1260=21*60

1827=21*87

2187=27*81

* 比較笨的低效率的做法: 遍歷所有四位數, 每生成一個四位數的時候,

* 在雙重循環遍歷兩位數,在兩位數的內層循環中判斷是否與最外層循環的四位數相等。 如果相等把這些數字都存放到數組,進行排序后比較

* 兩組數字,如果相等那么輸出這個數就是要找的數字;

*/

了解下這個英文參考:吸血鬼數字

An important theoretical result found by Pete Hartley:

1. If x·y is a vampire number then x·y == x+y (mod 9) Proof: Let mod be the binary modulo operator and d(x) the sum of the decimal

digits of x.

2. It is well-known that d(x) mod 9 = x mod 9, for all x.

Assume x·y is a vampire.

3.Then it contains the same digits as x and y,and in particular d(x·y) = d(x)+d(y).

4.This leads to:

(x·y) mod 9 = d(x·y) mod 9 = (d(x)+d(y)) mod 9 = (d(x) mod 9 + d(y) mod 9) mod 9 = (x mod 9 + y mod 9) mod 9 = (x+y) mod 9

The solutions to the congruence are (x mod 9, y mod 9) in {(0,0),

(2,2), (3,6), (5,8), (6,3), (8,5)} Only these cases (6 out of 81) have

to be tested in a vampire search based on testing x·y for different

values of x and y.

public class Exercise10 {

JAVA實現:

public static void main(String[] args) {

// TODO 自動生成的方法存根

int num1, num2, result, i, j,

count = 0;

int[] predata = new int[4];

int[] lastdata = new int[4];

for(num1 = 10; num1 < 99; num1++)

for(num2 = num1; num2 < 99; num2++){

result = num1 *num2;

count = 0;

if(((num1 * num2) %9) !=((num1 + num2) %9))

continue;

predata[0] = num1 / 10;

predata[1] = num1 % 10;

predata[2] = num2 / 10;

predata[3] = num2 % 10;

lastdata[0] = result / 1000;

lastdata[1] = (result % 1000) / 100;

lastdata[2] = (result % 1000 % 100) / 10;

lastdata[3] = (result % 1000 % 100 % 10);

for(i = 0; i < 4 ; i++)

for(j = 0; j < 4; j++){

if(predata[i] == lastdata[j]){

count++;

predata[i] = -1;

lastdata[j] = -2;

}

}

if(count == 4)

System.out.println(num1 +" * " + num2 + " = "+ result);

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值