当前位置:网站首页>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;
}
边栏推荐
- PHP wechat scan code, follow official account and authorize login source code
- Drive board network cable directly connected to computer shared network configuration
- Scaffold installation
- [QNX hypervisor 2.2 user manual]9.8 load
- Source code of YY music wechat applet imitating Netease cloud music
- Enterprise level inventory management system of code audit
- anaconda安装教程环境变量(如何配置环境变量)
- Analysis of the influence of ESM direction finding error on positioning error
- R language drawing parameters (R language plot drawing)
- 电商RPA,大促轻松上阵的法宝
猜你喜欢

Family relationship calculator wechat applet source code

5 ROS simulation modeling (3-rviz+gazebo+ control simulation robot)

Custom MVC principle

Node基础

AI chief architect 12 AICA industrial landing analysis under the industrial production process optimization scenario

Week 2: convolutional neural network

MVVM model

向下扎根,向上生长,探寻华为云AI的“根”力量

Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download

Analysis of direction finding error of multi baseline interferometer system
随机推荐
Mongodb的特点、与MySQL的差别、以及应用场景
Expression of directional signal -- complex exponential signal
模拟实现string类常用接口
Tencent map API request source is not authorized, this request source domain name
[QNX hypervisor 2.2 user manual]9.8 load
firewall 命令简单操作
[QNX hypervisor 2.2 user manual]9.7 generate
The fifth article in the series of radar Fundamentals: the function of radar modulation style
anaconda安装教程环境变量(如何配置环境变量)
PHP binary array is sorted by a field in it
自定义mvc原理
Classes and objects (3)
MathType installation and solution cannot solve the problem of crtl+v
Pytorch data input format requirements and conversion
动态内存管理
The small icon of notification setting shows a small square
serialization and deserialization
Simulink学习笔记(三)——Simulink自动代码生成(二)「建议收藏」
E-commerce RPA, a magic weapon to promote easy entry
Simulink learning notes (III) - Simulink automatic code generation (II) "suggestions collection"