173 + 286 = 459 295 + 173 = 468 173 + 295 = 468 183 + 492 = 675
以上都是正确的填写法! 注意:
111 + 222 = 333 是错误的填写法!
因为每个数字必须是不同的!
也就是说:1~9中的所有数字,每个必须出现且仅出现一次! 注意:
不包括数字“0”!
注意:
满足加法交换率的式子算两种不同的答案。 所以答案肯定是个偶数! 注意:
只要求计算不同的填法的数目 不要求列出所有填写法 更不要求填写源代码!
package Question10_19; import java.util.Vector; publicclass Question12 {
publicstaticintcount;
publicstaticvoid AllType(Vector
publicstaticvoid main(String[] args) {
Vector
31
if(sourse.size()==0){ }
//System.out.println(result);
int a=(result.elementAt(0)-'0')*100+(result.elementAt(1)-'0')*10+result.elementAt(2)-'0'; int b=(result.elementAt(3)-'0')*100+(result.elementAt(4)-'0')*10+result.elementAt(5)-'0'; int c=(result.elementAt(6)-'0')*100+(result.elementAt(7)-'0')*10+result.elementAt(8)-'0'; if(a+b==c){ }
for (int i = 0; i < sourse.size(); i++) { }
result.add(sourse.elementAt(i)); sourse.remove(i); AllType(sourse, result);
sourse.add(i, result.elementAt(result.size()-1)); result.remove(result.size()-1);
System.out.printf(\,a,b,c); count++;
}else{
}
}
}
sourse.add((char)('0'+i));
AllType(sourse, result); System.out.println(count);
运行结果:
124 + 659 = 783 125 + 739 = 864 127 + 359 = 486 ????
782 + 163 = 945 783 + 162 = 945 784 + 152 = 936 336
其他方法:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class T01 {
static List
public static void print(){ for(int[] x:lis){ for(int y:x){
System.out.print(y); }
System.out.println(); } }
// 检测是否有重复元素
public static boolean check2(int[] n){ if(lis.size()==0) return false; for(int i=0;i int b = lis.get(i)[3]*100+lis.get(i)[4]*10+lis.get(i)[5]; if(a!=b){ return false; } } return true; } // 检测,并添加符合的组合 public static void check(int[] n){ int a = n[0]*100+n[1]*10+n[2]; int b = n[3]*100+n[4]*10+n[5]; 32 int c = n[6]*100+n[7]*10+n[8]; if(a+b==c){ if(!check2(n)){ // 如果不重复,则添加 lis.add(Arrays.copyOf(n, n.length)); } } } // 全排列 public static void f(int[] n,int start,int end){ if(start>=end){ check(n); // 检测,并添加符合的组合 return ; }else{ for(int i=start;i public static void main(String[] args){ int[] n = {1,2,3,4,5,6,7,8,9}; f(n,0,n.length-1); print(); // 输出结果 System.out.println(\总个数:\ } } 运行结果: 124659783 125739864 127359486 127368495 128439567 128367495 ......... ......... ......... ......... ......... 745218963 746235981 输出个数33 748215963 754182936 752184936 763182945 762183945 715248963 716238954 718245963 718236954 783162945 784152936 782154936 782163945 总个数:336 方法二:另一种方法,下列是0-9的组合,没排除0 public class Demo01 { static int count=0; public static void main(String[] args){ for(int i=1;i<=9;i++){ // 第一个数的百位 for(int j=0;j<=9;j++){ // 十位 for(int k=0;k<=9;k++){ // 个位 for(int l=1;l<=9;l++){ // 第二个数的百位 for(int m=0;m<=9;m++){ // 十位 for(int n=0;n<=9;n++){ // 个位 if(i!=j && i!=k && i!=l && i!=m && i!=n && j!=k && j!=l && j!=m && j!=n && k!=l && k!=m && k!=n && l!=m && l!=n && m!=n ){ int a = i*100+j*10+k; int b = l*100+m*10+n; int c = a + b; int x,y,z; if(c>99&&c<999){ x = c/100; y = c0/10; z = c; }else{ break; } if(x!=y&&x!=z&&y!=z && x!=i && x!=j && x!=k && x!=l && x!=m && x!=n && y!=i && y!=j && y!=k && y!=l && y!=m && y!=n && z!=i && z!=j && z!=k && z!=l && z!=m && z!=n ){ 34 System.out.println(a+\ count++; } } } } } } } } System.out.println(\总个数:\ } } 运行结果: 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 ... + ... = ... ... + ... = ... ... + ... = ... ... + ... = ... ... + ... = ... 784 + 136 = 920 784 + 152 = 936 786 + 134 = 920 805 + 137 = 942 806 + 147 = 953 807 + 135 = 942 807 + 146 = 953 835 + 107 = 942 837 + 105 = 942 846 + 107 = 953 847 + 106 = 953 总个数:1088 35