762. 二进制表示中质数个计算置位

762. 二进制表示中质数个计算置位

给定两个整数 L 和 R ,找到闭区间 [L, R] 范围内,计算置位位数为质数的整数个数。

(注意,计算置位代表二进制表示中1的个数。例如 21 的二进制表示 10101 有 3 个计算置位。还有,1 不是质数。)

示例 1:

输入: L = 6, R = 10
输出: 4
解释:
6 -> 110 (2 个计算置位,2 是质数)
7 -> 111 (3 个计算置位,3 是质数)
9 -> 1001 (2 个计算置位,2 是质数)
10-> 1010 (2 个计算置位,2 是质数)
示例 2:

输入: L = 10, R = 15
输出: 5
解释:
10 -> 1010 (2 个计算置位, 2 是质数)
11 -> 1011 (3 个计算置位, 3 是质数)
12 -> 1100 (2 个计算置位, 2 是质数)
13 -> 1101 (3 个计算置位, 3 是质数)
14 -> 1110 (3 个计算置位, 3 是质数)
15 -> 1111 (4 个计算置位, 4 不是质数)

 

代码

#include<iostream>
#include<bits/stdc++.h>
#include<cstring>
#include<vector>
using namespace std;

int getCount(int n){
    int count=0;
    while(n>0){
        n=n&(n-1);
        count++;
    }
    return count;

}
int isPrime(int n){
    if (n==1)
        return 0;
    if(n==2) return 1;
    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n%i==0)
        {
            return 0;
        }
    }
    return 1;    
}

int main(){

    int L,R,count=0;
    cin>>L>>R;
    for (int i = L; i <= R; i++)
    {
        if(isPrime(getCount(i)))count++;
    }
    cout<<count<<endl;
    
}

 

方法一:

算法:

从 L 到 R,我们首先计算该数字转换为二进制有多少个 1。如果数量是 2, 3, 5, 7, 11, 13, 17, 19,则我们增加计数。最高是 19 的原因是 
R106<220

class Solution {
    public int countPrimeSetBits(int L, int R) {
        int ans = 0;
        for (int x = L; x <= R; ++x)
            if (isSmallPrime(Integer.bitCount(x)))
                ans++;
        return ans;
    }
    public boolean isSmallPrime(int x) {
        return (x == 2 || x == 3 || x == 5 || x == 7 ||
                x == 11 || x == 13 || x == 17 || x == 19);
    }
}

 

因上求缘,果上努力~~~~ 作者:每天卷学习,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/13548510.html

原文地址:https://www.cnblogs.com/BlairGrowing/p/13548510.html