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

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 import java.util.Map.Entry;
 4 //请在小于99999的正整数中找符合下列条件的数,它既是完全平方数,
 5 //又有两位数字相同,如:144,676。
 6 public class wqs {
 7     
 8     //完全平方数
 9     public static boolean iswqs(int n){
10         int i;
11         double dn=Math.sqrt(n);
12         if(dn-(int)dn==0)
13             return true;
14         return false;
15     }
16     //判断只有两位相同
17     public static boolean twoSame(int n){
18     Map<Integer,Integer>map=new HashMap<Integer,Integer>();
19         while(n>0){
20             int n1=n%10;
21 //            System.out.print(n1);
22             if(map.containsKey(n1))
23             {
24                 int val=map.get(n1);
25                 map.put(n1,val+1);
26             }
27             else
28                   map.put(n1, 1);
29             n=n/10;
30         }
31 //        System.out.print(map);
32         for(Entry<Integer,Integer>en:map.entrySet())
33         { 
34             if(en.getValue()==2){
35                 return true;
36             }
37         }
38         
39         return false;
40     }
41     
42     public static void main(String[] args) {
43     int i;
44         for(i=2;i<99999;i++){
45             if(twoSame(i)&&iswqs(i)){
46                 System.out.println(i+" ");
47             }
48         }
49     }
50 
51 }

 

posted @ 2016-10-15 17:27  游离的心  阅读(1196)  评论(0编辑  收藏  举报