今天是个好日子!问题实质如下:有一个数n及其二进制表示,经过一定次数的循环右移后,再次重复二进制表示,需要找出得到的最大数从这些转变之一的十进制表示。
这是我试图做的
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
using namespace std;
const int N = 1005;
string to_binary(int x) {
string s;
do {
s.push_back('0' + (x & 1));
} while (x >>= 1);
reverse(s.begin(), s.end());
return s;
}
int get_count_of_repeats(string s) {
if (s.size() == 1) {
return 0;
}
else {
string z = s;
int count = 1;
char c = z[z.size() - 1];
for (int i = z.size() - 1; i >= 1; i--) {
z[i] = z[i - 1];
}
z[0] = c;
while (s != z) {
char ch = z[z.size() - 1];
for (int i = z.size() - 1; i >= 1; i--) {
z[i] = z[i - 1];
}
z[0] = ch;
count++;
}
return count;
}
}
int main() {
int n;
cin >> n;
string binary = to_binary(n);
if (get_count_of_repeats(binary) == 0) {
cout << n << endl;
}
else {
int mx = -1000007;
int count = get_count_of_repeats(binary);
for (int i = 0; i < count; i++) {
string z = binary;
char c = z[z.size() - 1];
for (int j = z.size() - 1; j >= 1; j--) {
z[i] = z[i - 1];
}
z[0] = c;
mx = max(mx, stoi(z, nullptr, 2));
binary = z;
}
cout << mx << endl;
}
return 0;
}
但它给了我以下错误
请帮我解决问题

主要是
应该
复制粘贴和单字母变量造成了损害
>>通过对数字本身(整数变量)进行按位移位(和<<)和操作的操作,该问题以自然的方式解决,无需将数字转换为字符串|。例如,对于向右循环移位,您需要将数字的所有位向右移动一位,并将最年轻的(移位前)的值写入最高位。
如果循环移位只需要在原始数字的有效位范围内进行(例如,对于数字 5 (101b),这是 3 位),则添加一个计算此范围的函数
旋转的位移量计算为
shift_size = get_bitsize(n) - 1我们将通过屏蔽“额外”(由移位产生的)位来向右移位 -
((v >> 1) | (v << shift_size)) & mask),其中
mask由范围等于 1 的位组成。(2 的幂(即
1 << get_bitsize(n))负 1 构成相应的掩码)。一般来说,我们得到这部分代码进行迭代