马虎的算式

来源:互联网 发布:java项目打包成war 编辑:程序博客网 时间:2024/06/08 17:11

马虎的算式


小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。

有一次,老师出的题目是:36 x 495 = ?

他却给抄成了:396 x 45 = ?

但结果却很戏剧性,他的答案竟然是对的!!

因为 36 * 495 = 396 * 45 = 17820

类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54


假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)

能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?


请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。

满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。


import java.util.ArrayList;import java.util.List;public class Main {private static int count = 0;public static void main(String[] args) {int n = 5;List<Integer> list = new ArrayList<Integer>();for (int i = 0; i < n; i++) {list.add(0);// 全部初始化为0}numberCombination(list, n);System.out.println(count);}// 5个数的全排列public static void numberCombination(List<Integer> list, int n) {if (n <= 0) {checkEqual(list);return;}for (int i = 1; i <= 9; i++) {if (!list.contains(i)) {list.set(list.size() - n, i);} else {continue;}numberCombination(list, n - 1);// 递归list.set(list.size() - n, 0);}}// 检查两个数是否相等public static void checkEqual(List<Integer> list) {int a = list.get(0);int b = list.get(1);int c = list.get(2);int d = list.get(3);int e = list.get(4);int num1 = (a * 10 + b) * (c * 100 + d * 10 + e);int num2 = (a * 100 + d * 10 + b) * (c * 10 + e);if (num1 == num2) {count++;}}}
0 0