吸血鬼数字

234次阅读
没有评论

共计 1315 个字符,预计需要花费 4 分钟才能阅读完成。

  • 吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序.
  • 以两个0结尾的数字是不允许的。
  • 例如下列数字都是吸血鬼数字
  • 1260=21*60
  • 1827=21*87
  • 2187=27*81
package cn.peiluming.test;

import java.util.Arrays;

/**
 * 找出所有吸血鬼数字
 *
 * @author plm
 * @create 2020/12/10 17:46
 */
public class VampireNumberDemo1 {
    public static void main(String[] args) {
        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);
            }

    }
}

class VampireNumberDemo2 {
    public static void main(String[] args) {
        String[] ar_str1, ar_str2;
        int sum = 0;
        for (int i = 10; i < 100; i++) {
            for (int j = i; j < 100; j++) {
                int i_val = i * j;
                if (i_val < 1000 || i_val > 9999) {
                    continue; //积小于1000或大于9999排除,继续下一轮环
                }
                ar_str1 = ("" + i_val).split("");
                ar_str2 = (i + "" + j).split("");
                Arrays.sort(ar_str1);
                Arrays.sort(ar_str2);
                if (Arrays.equals(ar_str1, ar_str2)) {
                    //排序后比较,为真则找到一组
                    sum++;
                    System.out.println("第" + sum + "组: " + i + "*" + j + "=" + i_val);
                }
            }
        }
        System.out.println("共找到" + sum + "组吸血鬼数");
    }
}
正文完
 
裴先生
版权声明:本站原创文章,由 裴先生 2020-12-10发表,共计1315字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
本站勉强运行: