java . 请在小于99999的正整数中找符合下列条件的数,它既是完全平方数,又有两位数字相同,如:144,676。

时间:2023-03-09 08:18:47
java . 请在小于99999的正整数中找符合下列条件的数,它既是完全平方数,又有两位数字相同,如:144,676。
 import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
//请在小于99999的正整数中找符合下列条件的数,它既是完全平方数,
//又有两位数字相同,如:144,676。
public class wqs { //完全平方数
public static boolean iswqs(int n){
int i;
double dn=Math.sqrt(n);
if(dn-(int)dn==0)
return true;
return false;
}
//判断只有两位相同
public static boolean twoSame(int n){
Map<Integer,Integer>map=new HashMap<Integer,Integer>();
while(n>0){
int n1=n%10;
// System.out.print(n1);
if(map.containsKey(n1))
{
int val=map.get(n1);
map.put(n1,val+1);
}
else
map.put(n1, 1);
n=n/10;
}
// System.out.print(map);
for(Entry<Integer,Integer>en:map.entrySet())
{
if(en.getValue()==2){
return true;
}
} return false;
} public static void main(String[] args) {
int i;
for(i=2;i<99999;i++){
if(twoSame(i)&&iswqs(i)){
System.out.println(i+" ");
}
}
} }