题目:()()()+()()()=()()(),请将 1~9 这 9 个数字填入 9 个括号中并使等式两边成立,每个数字只能使用一次;
代码:
public class Test { | |
public static void main(String[] args) { | |
test002(); | |
} | |
/** | |
* 测试二 | |
* 问题描述:()()()+()()()=()()() | |
* 请将 1~9 这 9 个阿拉伯数字分别填入 9 个括号中使等式两边成立,每个数字只能使用一次 | |
*/ | |
private static void test002(){ | |
int a,b,c,d,e,f,g,h,i; | |
for ( int m=100;m<1000;m++ ){ | |
for ( int n=100;n<1000;n++ ){ | |
for ( int o=100;o<1000;o++ ){ | |
if ( (m+n)==o ){ | |
a = m/100; | |
b = (m%100)/10; | |
c = m-100*a-10*b; | |
d = n/100; | |
e = (n%100)/10; | |
f = n-100*d-10*e; | |
g = o/100; | |
h = (o%100)/10; | |
i = o-100*g-10*h; | |
int[] arr = {a,b,c,d,e,f,g,h,i}; | |
boolean bol = checkArrRepeat(arr); | |
if ( !bol ){ | |
System.out.println(m+","+n+","+o); | |
} | |
} | |
} | |
} | |
} | |
} | |
/** | |
* 判断数组是否重复 | |
* | |
* @param arr 入参 | |
* @return 有重复返回 true,无重复返回 false | |
*/ | |
private static boolean checkArrRepeat( int[] arr ){ | |
for ( int i=0;i<arr.length;i++ ){ | |
for ( int j=i+1;j<arr.length;j++ ){ | |
if ( arr[i]==arr[j] ){ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
} |
答案:
103,469,572 | |
103,479,582 | |
103,579,682 | |
103,649,752 | |
103,749,852 | |
103,759,862 | |
104,579,683 | |
104,759,863 | |
105,269,374 | |
105,279,384 | |
105,387,492 | |
105,629,734 | |
... ... | |
... ... |