检查数字在C ++中是否为荒诞
在这里,我们将看到一个数字是否为Bleak。如果一个数字不能表示为正数x的总和,且不能在x中设置位数,则称该数字为空白。因此,对于任何非负数x,x+set_bit_count(x)不等于n。
这个概念非常简单,如果设置的位数+数字与数字不同,则为“泄漏”,否则为“不”。
示例
#include <iostream>
using namespace std;
int set_bit_count(int x) {
unsigned int bit_count = 0;
while (x != 0) {
x &= (x - 1);
bit_count++;
}
return bit_count;
}
bool isBleakNumber(int n) {
for (int i = 1; i < n; i++)
if (i + set_bit_count(i) == n)
return false;
return true;
}
int main() {
isBleakNumber(3) ? cout << "Yes\n" : cout << "No\n";
isBleakNumber(4) ? cout << "Yes\n" : cout << "No\n";
}输出结果
No Yes
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语