猜算式

    xiaoxiao2021-03-25  143

    看下面的算式:

     

    □□ x □□ = □□ x □□□

     

    它表示:两个两位数相乘等于一个两位数乘以一个三位数。

     

    如果没有限定条件,这样的例子很多。

     

    但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。

    该算式中1至9的每个数字出现且只出现一次!

     

    比如:

    46 x 79 = 23 x 158

    54 x 69 = 27 x 138

    54 x 93 = 27 x 186

    .....

     

    请编程,输出所有可能的情况!

     

    注意:

    左边的两个乘数交换算同一方案,不要重复输出!

    不同方案的输出顺序不重要

    这是一个很水的方法,应该还可以利用全排列快速简洁的写出来。

    public static void main(String[] args){ Test9 test9=new Test9(); test9.sort(); } public void sort(){ ArrayList<B1> g=new ArrayList<B1>(); for(int a1=1;a1<10;a1++){ for(int a2=1;a2<10;a2++){ for(int b1=1;b1<10;b1++){ for(int b2=1;b2<10;b2++){ for(int c1=1;c1<10;c1++){ for(int c2=1;c2<10;c2++){ for(int d1=1;d1<10;d1++){ for(int d2=1;d2<10;d2++){ for(int d3=1;d3<10;d3++){ if(a1!=a2&&a1!=b1&&a1!=b2&&a1!=c1&& a1!=c2&&a1!=d1&&a1!=d2&&a1!=d3&& a2!=b1&&a2!=b2&&a2!=c1&&a2!=c2&&a2!=d1&&a2!=d2&& a2!=d3&&b1!=b2&&b1!=c1&&b1!=c2&&b1!=d1 &&b1!=d2&&b1!=d3&&b2!=c1&&b2!=c2&&b2!=d1 &&b2!=d2&&b2!=d3&&c1!=c2&&c1!=d1&&c1!=d2 &&c1!=d3&&c2!=d1&&c2!=d2&&c2!=d3&&d1!=d2 &&d1!=d3&&d2!=d3){ if((a1*10+a2)*(b1*10+b2)==(c1*10+c2)*(d1*100+d2*10+d3)){ int b=0; for(B1 d11:g){ if((a1*10+a2==d11.a&&b1*10+b2==d11.b)||(a1*10+a2==d11.b&&b1*10+b2==d11.a)){ b=1; continue; }else{ } } if(b!=1){ System.out.println((a1*10+a2)+"+"+(b1*10+b2)+"*"+"="+(c1*10+c2)+"*"+(d1*100+d2*10+d3)); g.add(new B1((a1*10+a2),(b1*10+b2))); } } } } } } } } } } } } } class B1{ int a; int b; public B1(int a,int b){ this.a=a; this.b=b; } }

    转载请注明原文地址: https://ju.6miu.com/read-23835.html

    最新回复(0)