这个问题是在我写个的几个博客里较为复杂的一个。首先,先看看整个问题的表述。
星号表示0~9的一个数字,而且不允许重复,使得下面的加法表达式成立。输出所有结果。
※ ※ ※ ※ ※
+ 2 0 0 8 5
————————
※ ※ ※ ※ ※
---------------------------------------------------分割线---------------------------------------------------------
对该问题进行分析(从简,自己在纸上画一画肯定能弄懂的):
用0~9 10个数字填入下列未知部分,不能重复使用,使等式成立
a b c d e
+ 2 0 0 8 5
———————
f g h i j
-------------------------
分析:
a+2不能进位
b不能和g相等,故后面往前进了一位
c不能和h相等,故后面往前进了一位,因此,c为9,h为0.
d+8(+1)要进位,且i!=0,d为3,4,5,6,7,8
综上分析知:
a b 9 d e
+ 2 0 0 8 5
—————————
a+2 b+1 0 i j
------------------------------
9和0已经出现,所以
a为123456中一个
b为1234567中一个
d为345678中一个
e为123678中一个
i=(a+8)%10或(a+9)%10(进位)
j为678123中一个
根据循环即可求解。
求解过程中,注意e分为123,678两种进行循环
将数字分为两个数组,加式数组adder[5]和结果数组result[5]。
在循环中往里面加入数字,两个数组每个元素取出,连接成字符串,判断字符串数组中10个数字是否重复,如果不重复则是本题解,输出,否则继续循环。
上代码。
package com.fuxuemingzhu.evaluated.main; import java.util.HashMap; import java.util.Iterator; /** * <p> * Title: Main * </p> * <p> * Description:计算0~9数字不重复使等式成立的排列 * </p> * * @author fuxuemingzhu * * @email fuxuemingzhu@163.com * * @date 2014年11月24日 下午10:46:22 */ public class Main { /** * adder 加式因子 */ private static int[] adder = new int[5]; /** * result 加式结果 */ private static int[] result = new int[5]; /** * num 使等式成立的组数 */ private static int num = 0; /** * anserString 为了校验等式10个数是否重复设立的字符串 */ private static String anserString = new String(); /** * charsCount 为了校验等式10个数是否重复设立的HashMap */ public static HashMap<String, Integer> charsCount = new HashMap<String, Integer>(); /** * <p> * Title: main * </p> * <p> * Description:main()方法 * </p> * * @param args * */ public static void main(String[] args) { cuculate(); } /** * <p> * Title: cuculate * </p> * <p> * Description:利用循环计算出所有可能的结果 * </p> * */ public static void cuculate() { adder[2] = 9; result[2] = 0; for (int i = 1; i <= 6; i++) { adder[0] = i; result[0] = adder[0] + 2; for (int j = 1; j <= 7; j++) { adder[1] = j; result[1] = adder[1] + 1; for (int j2 = 3; j2 <= 8; j2++) { adder[3] = j2; for (int k = 1; k <= 3; k++) { result[3] = (adder[3] + 8) % 10; adder[4] = k; result[4] = adder[4] + 5; if (check()) { num++; output(); } } for (int k = 6; k <= 8; k++) { result[3] = (adder[3] + 9) % 10; adder[4] = k; result[4] = (adder[4] + 5) % 10; if (check()) { num++; output(); } } } } } } /** * <p> * Title: check * </p> * <p> * Description:判断是否满足10个数字不重复的条件 * </p> * * @return * */ public static boolean check() { anserString = new String(); arrays2String(); countWords(); return checkMap(); } /** * <p> * Title: arrays2String * </p> * <p> * Description:把数组转换成字符串,方便调用函数校验 * </p> * */ public static void arrays2String() { for (int i : adder) { anserString += i; } for (int i : result) { anserString += i; } } /** * <p> * Title: countWords * </p> * <p> * Description:用HashMap保存每个字符出现的次数 * </p> * */ public static void countWords() { charsCount = new HashMap<String, Integer>(); for (int i = 0; i < anserString.length(); i++) { String temp = anserString.substring(i, i + 1); if (charsCount.containsKey(temp)) { charsCount.put(temp, charsCount.get(temp) + 1); } else { charsCount.put(temp, 1); } } } /** * <p> * Title: checkMap * </p> * <p> * Description:将HashMap中的每一个数字的出现次数进行判断,所有数字都出现1次,返回true,否则返回false * </p> * */ public static boolean checkMap() { for (Iterator<String> iterator = charsCount.keySet().iterator(); iterator .hasNext();) { String words = (String) iterator.next(); int num = charsCount.get(words); if (num > 1) { return false; } } return true; } /** * <p> * Title: output * </p> * <p> * Description:结果的输出 * </p> * */ public static void output() { System.out.println(" 这是第" + num + "组解:"); String outputString = new String(" "); for (int i : adder) { outputString += i + " "; } System.out.println(outputString); System.out.println(" + 2 0 0 8 5\n—————————————"); outputString = new String(" "); for (int i : result) { outputString += i + " "; } System.out.println(outputString); System.out.println("-------------------"); } }
还需要解释一下其中识别0~9十个数字是否重复使用的解决方法。我是根据把数组里的元素转化成字符串,判断字符串是否有重复元素判断的,因为这个方法在我前面的博客里已经有使用,所以这里直接拿过来用了,图个方便。可参考我的另一篇博客:java源码——统计字符串中字符出现的次数。
这里是程序运行时得到的本问题的所有解,共6组:
这是第1组解:
1 4 9 8 2
+ 2 0 0 8 5
——————
3 5 0 6 7
-------------------
这是第2组解:
2 7 9 5 1
+ 2 0 0 8 5
———————
4 8 0 3 6
-------------------
这是第3组解:
3 7 9 4 1
+ 2 0 0 8 5
———————
5 8 0 2 6
-------------------
这是第4组解:
4 1 9 7 3
+ 2 0 0 8 5
———————
6 2 0 5 8
-------------------
这是第5组解:
5 1 9 6 3
+ 2 0 0 8 5
———————
7 2 0 4 8
-------------------
这是第6组解:
6 4 9 3 2
+ 2 0 0 8 5
———————
8 5 0 1 7
-------------------
附运行截图。