我们是数字N。目标是找到0到N之间的数字,其与N的或等于与N的XOR。
我们将通过遍历否来做到这一点。从i = 0到i <= N,对于每个i,如果(N ^ i == i | N),则递增计数。
让我们通过示例来理解。
输入-X = 6
输出-N的OR == N的XOR的数字计数:2
说明-数字为0 1。
输入-X = 20
输出-与N的OR ==与N的XOR的数字计数
说明-数字是0 1 2 3 8 9 10 11
我们取整数N。
函数orisXOR(int n)取n并返回一个数的计数,其与n的或等于与n的XOR。
将初始计数设为0。
从i = 0到i <= n遍历。
如果i | n == i ^ n。增量计数
在for循环结束时,计数将达到所需的结果。
返回计数并打印。
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int orisXOR(int n){
int count = 0;
for (int i = 0; i <= n; i++){
if((n|i)==(i^n))
{ count++; }
}
return count;
}
int main(){
int N = 15;
int nums=orisXOR(N);
cout <<endl<<"Count of numbers whose OR with N == XOR with N: "<<nums;
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
Count of numbers whose OR with N == XOR with N: 1