//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;
}
}
Java - 趣味题(4) --- 魔术乘法算式
最新推荐文章于 2021-02-16 09:35:37 发布