題目:
小明正看着 203879 這個數字發呆。
原來,203879 * 203879 = 41566646641
這有什麼神奇呢?仔細觀察,203879 是個6位數,並且它的每個數位上的數字都是不同的,
並且它平方後的所有數位上都不出現組成它自身的數字。
具有這樣特點的6位數還有一個,請你找出它!
再歸納一下篩選要求:
要求1:6位正整數
要求2:每個數位上的數字不同
要求3:其平方數的每個數位不含原數字的任何組成數位
實現代碼:
package com.dj.zuoy1;
import java.util.*;
public class Number {
// 判斷不同
public static Set bt(long x) {
Set t = new HashSet(); // hashset集合
String s = "" + x;
for(int i=0; i<s.length(); i++) {
t.add(s.charAt(i)); // charAt() 方法可返回指定位置的字符。
}
return t;
}
public static boolean f(int x) {
long y = (long)x * x;
Set a = bt(x);
Set b = bt(y);
if(a.size() != 6) {
return false;
}
a.retainAll(b);
// retainALL方法: 如果集合a中的元素都在集合b中則a中的元素不做移除操作,
// 反之如果只要有一個不在a中則會進行移除操作。
// 即:list進行移除操作返回值爲:true反之返回值則爲false。
return a.isEmpty();
//isEmpty() 方法分配了內存空間,值爲空,是絕對的空
}
public static void main(String[] args) {
final int A =100000;
final int B =1000000;
for(int i=A; i<B; i++) {
if(f(i))
System.out.println(i);
}
}
}