当前位置:网站首页>762. 二进制表示中质数个计算置位
762. 二进制表示中质数个计算置位
2022-07-25 23:21:00 【Mr Gao】
762. 二进制表示中质数个计算置位
给你两个整数 left 和 right ,在闭区间 [left, right] 范围内,统计并返回 计算置位位数为质数 的整数个数。
计算置位位数 就是二进制表示中 1 的个数。
例如, 21 的二进制表示 10101 有 3 个计算置位。
示例 1:
输入:left = 6, right = 10
输出:4
解释:
6 -> 110 (2 个计算置位,2 是质数)
7 -> 111 (3 个计算置位,3 是质数)
9 -> 1001 (2 个计算置位,2 是质数)
10-> 1010 (2 个计算置位,2 是质数)
共计 4 个计算置位为质数的数字。
示例 2:
输入:left = 10, right = 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 不是质数)
共计 5 个计算置位为质数的数字。
这题使用常规方法去做就可以了,写两个函数就行啦,解题代码如下:
int f(int n){
int count=0;
while(n){
if(n%2==1){
count++;
}
n=n/2;
}
return count;
}
bool f2(int count){
if(count<=3&&count>1){
return true;
}
if(count<=1){
return false;
}
for(int i=2;i<=count/2;i++){
if(count%i==0){
return false;
}
}
return true;
}
int countPrimeSetBits(int left, int right){
int count=0;
for(int i=left;i<=right;i++){
if(f2(f(i))){
count++;
}
}
return count;
}
边栏推荐
- Network Security Learning notes-1 file upload
- TS basic data type
- Redis过期键的删除策略[通俗易懂]
- WordPress removes the website publishing time
- Thinkphp6 temporarily close the layout
- Longitude and latitude and its transformation with coordinate system
- 日期类的实现
- Tencent map API request source is not authorized, this request source domain name
- Enabling partners, how can Amazon cloud technology "get on the horse and get a ride"?
- Mongodb的特点、与MySQL的差别、以及应用场景
猜你喜欢

Unity 使用宏

Dynamic memory management

Tips for using (1)

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions

学习探索-波浪

类和对象(2)(6个默认成员函数)

学习探索-3d轮播卡片

谷粒学苑P98踩坑 e.GlobalExceptionHandler : null

Mongodb的特点、与MySQL的差别、以及应用场景

Secure code warrior learning record (II)
随机推荐
向下扎根,向上生长,探寻华为云AI的“根”力量
Hj7 take approximate value
谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
Take root downward, grow upward, and explore the "root" power of Huawei cloud AI
Mongodb的特点、与MySQL的差别、以及应用场景
Bind class style and bind style style
Thinkphp6 temporarily close the layout
CTS测试方法「建议收藏」
The difference between MySQL clustered index and non clustered index
Implementation of date class
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
Redis expiration key deletion strategy [easy to understand]
Serialize data type
日期类的实现
Tencent map API request source is not authorized, this request source domain name
Call Gaode map -- address is converted into longitude and latitude
Mongodb features, differences with MySQL, and application scenarios
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
学习探索-波浪
Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants