Java - 趣味题(4) --- 魔术乘法算式

//2016/11/14
//API:http://www.apihome.cn/api/android/Arrays.html

//实现一个乘法算式,他们的乘积仍然是这4个数组成的
//eg:
//210 * 6 = 1260;
//8 * 473 = 3784;
//27 * 81 = 2187;
//......
//求出所有的这种乘法算式,求出个数~

import java.util.Arrays;  

public class Java_Magic{  
    static int vis[] = new int[10];  
    static int bk[] = new int[10];  
    public static void main(String[] args){  
        int sum=0;  
        for(int i=1023;i<=9876;i++){  
            Arrays.fill(vis, 0);         //Fills the specified array with the specified element. 
            if(check(i)==0) continue;  
            bk= Arrays.copyOf(vis, vis.length);  //Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
            for(int j=1;j<=98;j++){  
                vis= Arrays.copyOf(bk, bk.length);   //Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. 
                if(i%j!=0)continue;  
                int k=i/j;  
                if(j>k)continue;  
                if(check2(k,j)==0) continue;  
                sum++;  
                System.out.println(j+"*"+k+"="+i);  
            }  
        }  
        System.out.println(sum);  
    }  

    //用于判断4个数字是否重复出现  
    static int check(int x) {  
          do {  
            if(vis[x % 10] != 0) return 0;  
            vis[x%10]++;  
            x=x/10;  
          } while(x!=0);  
          return 1;  
        }  

    //k与j的每一位数字都不允许重复,而且必须是i中已经出现了的数字  
    static int check2(int x,int y){  
        do{  
            if(vis[x%10]==0) return 0;  
            //保证相同的数字只出现一次,为对y的判断做铺垫  
            vis[x%10]--;  
            x=x/10;  
          }while(x!=0);  
        do{  
            if(vis[y%10]==0) return 0;  
            vis[y%10]--;  
            y=y/10;  
         }while(y!=0);  
        return 1;  
    }  
}  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值