1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     int countPrimeSetBits(int L, int R) 
12     {
13         int res=0;
14         for(int k=L;k<=R;k++)
15         {
16             int cur=countone(k);
17             if(judgeprime(cur))
18                 res++;
19         }
20         return res;
21     }
22     
23     int countone(int i)
24     {
25         int count=0;
26         while(i)
27         {
28             count++;
29             i&=i-1;
30         }
31         return count;
32     }
33     
34     bool judgeprime(int i)
35     {
36         if(i==1)
37             return false;
38         int last=sqrt(i);
39         for(int j=2;j<=last;j++)
40         {
41             if(i%j==0)
42                 return false;
43         }
44         return true;
45     }
46 };

三个函数,一个统计二进制1的个数,一个判定质数,一个统计结果

posted on 2018-06-12 21:00  高数考了59  阅读(127)  评论(0)    收藏  举报