在小於99999數裏面尋找能開根號的數,並且數裏面要有2個相同的數,如144,565這種數,不能把數轉爲字符串

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Test {
	
	//完全平方數
    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+" ");
            }
        }
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章