等距二进制判断
华为OD机试新系统真题 华为OD上机考试新系统真题 5月20号 100分题型
华为OD机试新系统真题目录点击查看: 华为OD机试真题题库目录|机考题库 + 算法考点详解
题目内容
对于一个二进制数,我们定义相邻两个 1 1 1 之间 0 0 0 的数量为他们两个之间的距离,如 1001011 1001011 1001011,相邻两个 1 1 1 之间的距离从左到右分别为 2 2 2、 1 1 1、 0 0 0。
现在如果一个整数转化为二进制数满足如下条件:
- 包含不少于 3 3 3个 1 1 1
- 所有相邻数字 1 1 1之间的距离相同 我们称之为等距二进制,如 21 21 21(二进制为: 10101 10101 10101)、 60 60 60(二进制为: 111100 111100 111100)、 146 146 146(二进制为: 10010010 10010010 10010010)。
输入描述
现给定一个输入,整数 0 0 0 <= n n n < ( 2 2 2^ 31 31 31 - 1 1 1)
输出描述
如果 n n n 是等距二进制,请输出它的距离,如果不是等距二进制,请输出 − 1 -1 −1
样例1
输入
21
输出
1
说明
二进制为 10101 10101 10101,距离为 1 1 1
样例2
输入
60
输出
0
说明
二进制为 111100 111100 111100,距离为 0 0 0
样例3
输入
146
输出
2
说明
二进制为 10010010 10010010 10010010,距离为 2 2 2
样例4
输入
2
输出
-1
说明
二进制为 10 10 10,不满足条件 1 1 1,非等距二进制
题解
思路:二进制处理
- 可以通过从低位到高位遍历给定
n来判断是不是等距二进制 - 初始定义
count = 0记录出现1的次数,distance = -1等距二进制数1的距离,定义last -1用来记录处理过程上一个1出现的位置。index记录当前index位置 - 可以通过
二进制移位进行二进制遍历,结束条件为n ==0,具体处理为- 通过与算法获取当前
index位的值value = n & 1,value == 0可直接跳过 - 如果当前
value == 1, 进行count += 1.接下来再处理等距逻辑判断,逻辑如下- 如果
last == -1,说明首次出现1,更新last=index即可。 - 如果
last != -1说明之前低位已存在1- 如果此时
distance == -1则确定等距为distance = index - last -1 - 如果
distance != -1, 则判断index - last - 1是否distance相同,不相同则可以直接确定不为等距二进制,直接返回-1
- 如果此时
- 如果
- 处理完等距逻辑之后,更新
last = index,对n >>= 1移位处理,index++
- 通过与算法获取当前
- 按照3处理之后,如果
count < 3,不满足条件1,直接返回-1.否则返回distance即可
c++
c++
#include<iostream>
#include<vector>
#include<string>
#include <utility>
#include <sstream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
int getEqualBinDistance(int n) {
// 二进制1的数量
int count = 0;
// 二进制位
int index = 0;
int distance = -1;
// 上一个1的位置
int last = -1;
while (n > 0) {
// 最末尾二进制值
int value = n & 1;
if (value == 1) {
count++;
if (last != -1) {
if (distance == -1) {
distance = index - last - 1;
} else {
// 不是等距
if (index - last-1 != distance) {
return -1;
}
}
}
last = index;
}
index++;
n >>= 1;
}
// 1数量小于3
if (count < 3) {
return -1;
}
return distance;
}
int main() {
int n;
cin >> n;
int res = getEqualBinDistance(n);
cout << res;
}
Java
JAVA
import java.util.*;
public class Main {
public static int getEqualBinDistance(int n) {
// 二进制1的数量
int count = 0;
// 二进制位
int index = 0;
int distance = -1;
// 上一个1的位置
int last = -1;
while (n > 0) {
// 最末尾二进制值
int value = n & 1;
if (value == 1) {
count++;
if (last != -1) {
if (distance == -1) {
distance = index - last - 1;
} else {
// 不是等距
if (index - last - 1 != distance) {
return -1;
}
}
}
last = index;
}
index++;
n >>= 1;
}
// 1数量小于3
if (count < 3) {
return -1;
}
return distance;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = getEqualBinDistance(n);
System.out.print(res);
}
}
Python
python
def getEqualBinDistance(n):
# 二进制1的数量
count = 0
# 二进制位
index = 0
distance = -1
# 上一个1的位置
last = -1
while n > 0:
# 最末尾二进制值
value = n & 1
if value == 1:
count += 1
if last != -1:
if distance == -1:
distance = index - last - 1
else:
# 不是等距
if index - last - 1 != distance:
return -1
last = index
index += 1
n >>= 1
# 1数量小于3
if count < 3:
return -1
return distance
n = int(input())
res = getEqualBinDistance(n)
print(res)
JavaScript
js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function getEqualBinDistance(n) {
// 二进制1的数量
let count = 0;
// 二进制位
let index = 0;
let distance = -1;
// 上一个1的位置
let last = -1;
while (n > 0) {
// 最末尾二进制值
let value = n & 1;
if (value === 1) {
count++;
if (last !== -1) {
if (distance === -1) {
distance = index - last - 1;
} else {
// 不是等距
if (index - last - 1 !== distance) {
return -1;
}
}
}
last = index;
}
index++;
n >>= 1;
}
// 1数量小于3
if (count < 3) {
return -1;
}
return distance;
}
rl.on("line", function(line) {
const n = Number(line);
const res = getEqualBinDistance(n);
console.log(res);
rl.close();
});
Go
go
package main
import "fmt"
func getEqualBinDistance(n int) int {
// 二进制1的数量
count := 0
// 二进制位
index := 0
distance := -1
// 上一个1的位置
last := -1
for n > 0 {
// 最末尾二进制值
value := n & 1
if value == 1 {
count++
if last != -1 {
if distance == -1 {
distance = index - last - 1
} else {
// 不是等距
if index-last-1 != distance {
return -1
}
}
}
last = index
}
index++
n >>= 1
}
// 1数量小于3
if count < 3 {
return -1
}
return distance
}
func main() {
var n int
fmt.Scan(&n)
res := getEqualBinDistance(n)
fmt.Print(res)
}
C语言
cpp
#include <stdio.h>
int getEqualBinDistance(int n) {
// 二进制1的数量
int count = 0;
// 二进制位
int index = 0;
int distance = -1;
// 上一个1的位置
int last = -1;
while (n > 0) {
// 最末尾二进制值
int value = n & 1;
if (value == 1) {
count++;
if (last != -1) {
if (distance == -1) {
distance = index - last - 1;
} else {
// 不是等距
if (index - last - 1 != distance) {
return -1;
}
}
}
last = index;
}
index++;
n >>= 1;
}
// 1数量小于3
if (count < 3) {
return -1;
}
return distance;
}
int main() {
int n;
scanf("%d", &n);
int res = getEqualBinDistance(n);
printf("%d", res);
return 0;
}