周期性二进制表示
Periodical binary representation
如何制作一个程序来检查给定整数的二进制表示是否是周期长度m>=2的周期性?
例如:
10的二进制表示是周期性
10 base 10 = 1010 base 2, periodical with period length 2
9的二进制表示不是周期性
9 base 10 = 1001 base 2
153的二进制表示是周期性
153 base 10 = 10011001 base 2, periodical with period length 4
有什么具体的算法可以做到这一点吗?
我在C++中工作。
您可以做的是旋转位,每次检查数字是否相等。即旋转的和您从开始的
// it is being assumed the size of int is 4bytes
int num = 153;
int temp = num;
int i = 0;
for (i=0; i<(8*sizeof(int))/2; i++){
temp = ((temp >> 1) & LONG_MAX | temp << 31) // rotate the bits by 1
if (!(temp ^ num)){ // check if the numbers are the same
break;
}
}
if (i<(8*sizeof(int))/2)
std::cout << "Period is" << i << "n";
else
std::cout << "Not periodic";
复杂度在位数上是线性的。
KMP是一种特定的算法,用于查找任何字符串的句点,包括数字的二进制表示,它只是一个字符串。它在O(n)时间内运行。
#include <iostream>
#include <algorithm>
using namespace std;
int calc_period(string s) {
vector<int> pre(s.size());
// this condition is keeped true in the for-loop:
// s[0..pre[i]] is this longest suffix of s[0..i] in s[0..i]'s all prefixes (if pre[i] >= 0)
int k = -1;
pre[0] = -1;
for (int i = 1; i < int(s.size()); ++i) {
while (k >= 0 && s[k + 1] != s[i]) {
k = pre[k];
}
if (s[k + 1] == s[i]) {
++k;
}
pre[i] = k;
}
int l = s.size() - 1 - pre[s.size() - 1];
return s.size() % l == 0 ? s.size() / l : 1;
}
string to_binary(long long x) {
string s;
if (x == 0) {
s = "0";
} else {
while (x) {
s += (x & 1) ? "1" : "0";
x >>= 1;
}
reverse(s.begin(), s.end());
}
return s;
}
int main() {
int x;
cin >> x;
cout << calc_period(to_binary(x)) << endl;
return 0;
}
您可以尝试一下这段代码,看看它是如何工作的。如果你想更深入地了解KMP,可以阅读它的wiki页面或相关的教科书,如"算法简介"。
相关文章:
- 浮点数为 32 位和 64 位二进制表示形式
- 如何将uint8_t的二进制表示形式读取为数字
- 二进制表示,如何获得"相关位"?
- C++ 如何以二进制格式表示字符串?
- 编译器C++如何在二进制代码中表示整数
- 了解浮点数的二进制表示形式
- 如何在 mfc c++ 中流式传输.zip文件夹的二进制表示形式
- 浮点逆二进制表示
- 以C++显示浮点类型的二进制表示形式
- 如何使用C 将二进制数字的字符串转换为其签名的十进制表示
- 在二进制表示中将二进制 std::string 转换为 int
- 如何在没有循环的二进制数字表示中只计算1位
- 如何在计算机内存中打印长双重表示的二进制表示
- 将uint8_t转换为其二进制表示
- 如何在C++中操作和表示二进制数
- 如何在C++中表示二进制字节数组数据
- 如何在编程语言中表示二进制字段
- 如何在C++中表示二进制数(用于霍夫曼编码器)
- 如何在表示二进制文件的字符串中查找整数
- 将表示二进制的字符串转换为表示等效十六进制的字符串