java编程思想中求吸血鬼数字的两种写法

class M{
    public static void main(String[] args) {
        int count = 0;
        String[] s_arr,n_arr;
        for (int i = 10; i < 100; i++) {
            for (int j = i; j < 100; j++) {
                int sum = i * j;
                if(sum > 1000 && sum < 10000 && (sum - i - j) % 9 == 0){
                    count++;
                    s_arr = String.valueOf(sum).split("");
                    n_arr = (String.valueOf(i) + String.valueOf(j)).split("");
                    Arrays.sort(s_arr);
                    Arrays.sort(n_arr);
                    if(Arrays.equals(s_arr,n_arr)){
                        System.out.println(sum+"="+i+"*"+j);
                    }
                }
            }
        }
        System.out.println(count);
    }
}

运行结果:

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

class L{
    public static void main(String[] args) {
        int count = 0,num = 0;
        for (int i = 10; i < 100; i++) {
            for (int j = i; j < 100; j++) {
                int sum = i * j;
                if(sum > 1000 && sum < 10000 && (sum - i - j) % 9 == 0){
                    count++;
                    int[] s_arr = new int[4];
                    int k = 0;
                    while(sum != 0){
                        s_arr[k++] = sum % 10;
                        sum /= 10;
                    }
                    int[] n_arr = new int[4];
                    n_arr[0] = i / 10;
                    n_arr[1] = i % 10;
                    n_arr[2] = j / 10;
                    n_arr[3] = j % 10;
                    int scount = 0;
                    for (int l = 0; l < 4; l++) {
                        for (int m = 0; m < 4; m++) {
                            num++;
                            if(s_arr[l] == n_arr[m]){
                              scount++;
                              s_arr[l] = -1;
                              n_arr[m] = -2;
                              if(scount == 4)
                                  System.out.println(i * j + "=" + i + "*" + j);
                            }
                        }
                    }

                }
            }
        }
        System.out.println(count);
        System.out.println(num);
    }
}

运行结果:

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

posted @ 2019-09-06 17:11  李润  阅读(277)  评论(0编辑  收藏  举报