欢迎来到Introzo百科
Introzo百科
计算与x的XOR大于x的较小值
给定整数“ x”,找到满足以下条件的“ a”值的数量:
例子 :
Input : x = 10
Output : 5
Explanation: For x = 10, following 5 values
of 'a' satisfy the conditions:
1 XOR 10 = 11
4 XOR 10 = 14
5 XOR 10 = 15
6 XOR 10 = 12
7 XOR 10 = 13
Input : x = 2
Output : 1
Explanation: For x=2, we have just one value
1 XOR 2 = 3.
天真的方法
一种简单的方法是检查“ a”在0到“ x”之间的所有值,并计算它与x的XOR,并检查条件1是否满足。
C++
// C++ program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
#include
using namespace std;
int countValues(int x)
{
int count = 0;
for (int i=1; i < x; i++)
if ((i ^ x) > x)
count++;
return count;
}
// Driver code
int main()
{
int x = 10;
cout << countValues(x);
return 0;
}
Java
// Java program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
public class XOR
{
static int countValues(int x)
{
int count = 0;
for (int i=1; i < x; i++)
if ((i ^ x) > x)
count++;
return count;
}
public static void main (String[] args)
{
int x = 10;
System.out.println(countValues(x));
}
}
// This code is contributed by Saket Kumar
Python3
# Python3 program to find
# count of values whose
# XOR with x is greater
# than x and values are
# smaller than x
def countValues(x):
count = 0
for i in range(1 ,x):
if ((i ^ x) > x):
count += 1
return count
# Driver code
x = 10
print(countValues(x))
# This code is contributed
# by Smitha
C#
// C# program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
using System;
class GFG
{
static int countValues(int x)
{
int count = 0;
for (int i = 1; i < x; i++)
if ((i ^ x) > x)
count++;
return count;
}
public static void Main ()
{
int x = 10;
Console.Write(countValues(x));
}
}
// This code is contributed by nitin mittal.
PHP
$x)
$count++;
return $count;
}
// Driver code
$x = 10;
echo countValues($x);
// This code is contributed by anuj_67.
?>
Javascript
C++
// C++ program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
#include
using namespace std;
int countValues(int x)
{
// Initialize result
int count = 0, n = 1;
// Traversing through all bits of x
while (x != 0)
{
// If current last bit of x is set
// then increment count by n. Here
// n is a power of 2 corresponding
// to position of bit
if (x%2 == 0)
count += n;
// Simultaneously calculate the 2^n
n *= 2;
// Replace x with x/2;
x /= 2;
}
return count;
}
// Driver code
int main()
{
int x = 10;
cout << countValues(x);
return 0;
}
Java
// Java program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
class GFG
{
static int countValues(int x)
{
// Initialize result
int count = 0, n = 1;
// Traversing through all bits of x
while (x != 0)
{
// If current last bit of x is set
// then increment count by n. Here
// n is a power of 2 corresponding
// to position of bit
if (x % 2 == 0)
count += n;
// Simultaneously calculate the 2^n
n *= 2;
// Replace x with x/2;
x /= 2;
}
return count;
}
// Driver code
public static void main (String[] args)
{
int x = 10;
System.out.println(countValues(x));
}
}
// This code is contributed by Saket Kumar
Python3
# Python3 program to find count
# of values whose XOR with
# x is greater than x and
# values are smaller than x
def countValues(x):
# Initialize result
count = 0;
n = 1;
# Traversing through
# all bits of x
while (x > 0):
# If current last bit
# of x is set then
# increment count by
# n. Here n is a power
# of 2 corresponding
# to position of bit
if (x % 2 == 0):
count += n;
# Simultaneously
# calculate the 2^n
n *= 2;
# Replace x with x/2;
x /= 2;
x = int(x);
return count;
# Driver code
x = 10;
print(countValues(x));
# This code is contributed
# by mits
C#
// C# program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
using System;
class GFG
{
static int countValues(int x)
{
// Initialize result
int count = 0, n = 1;
// Traversing through all bits of x
while (x != 0)
{
// If current last bit of x is set
// then increment count by n. Here
// n is a power of 2 corresponding
// to position of bit
if (x % 2 == 0)
count += n;
// Simultaneously calculate the 2^n
n *= 2;
// Replace x with x/2;
x /= 2;
}
return count;
}
// Driver code
public static void Main ()
{
int x = 10;
Console.Write(countValues(x));
}
}
// This code is contributed by nitin mittal
PHP
Javascript
输出 :
5
上述方法的时间复杂度为O(x)。
高效方法
有效的解决方案在于数字的二进制表示形式。我们以二进制表示考虑全0。对于第i个位置上的每个0,我们可以让2个小于或等于x的i数具有更大的XOR。
C++
// C++ program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
#include
using namespace std;
int countValues(int x)
{
// Initialize result
int count = 0, n = 1;
// Traversing through all bits of x
while (x != 0)
{
// If current last bit of x is set
// then increment count by n. Here
// n is a power of 2 corresponding
// to position of bit
if (x%2 == 0)
count += n;
// Simultaneously calculate the 2^n
n *= 2;
// Replace x with x/2;
x /= 2;
}
return count;
}
// Driver code
int main()
{
int x = 10;
cout << countValues(x);
return 0;
}
Java
// Java program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
class GFG
{
static int countValues(int x)
{
// Initialize result
int count = 0, n = 1;
// Traversing through all bits of x
while (x != 0)
{
// If current last bit of x is set
// then increment count by n. Here
// n is a power of 2 corresponding
// to position of bit
if (x % 2 == 0)
count += n;
// Simultaneously calculate the 2^n
n *= 2;
// Replace x with x/2;
x /= 2;
}
return count;
}
// Driver code
public static void main (String[] args)
{
int x = 10;
System.out.println(countValues(x));
}
}
// This code is contributed by Saket Kumar
Python3
# Python3 program to find count
# of values whose XOR with
# x is greater than x and
# values are smaller than x
def countValues(x):
# Initialize result
count = 0;
n = 1;
# Traversing through
# all bits of x
while (x > 0):
# If current last bit
# of x is set then
# increment count by
# n. Here n is a power
# of 2 corresponding
# to position of bit
if (x % 2 == 0):
count += n;
# Simultaneously
# calculate the 2^n
n *= 2;
# Replace x with x/2;
x /= 2;
x = int(x);
return count;
# Driver code
x = 10;
print(countValues(x));
# This code is contributed
# by mits
C#
// C# program to find count of values
// whose XOR with x is greater than x
// and values are smaller than x
using System;
class GFG
{
static int countValues(int x)
{
// Initialize result
int count = 0, n = 1;
// Traversing through all bits of x
while (x != 0)
{
// If current last bit of x is set
// then increment count by n. Here
// n is a power of 2 corresponding
// to position of bit
if (x % 2 == 0)
count += n;
// Simultaneously calculate the 2^n
n *= 2;
// Replace x with x/2;
x /= 2;
}
return count;
}
// Driver code
public static void Main ()
{
int x = 10;
Console.Write(countValues(x));
}
}
// This code is contributed by nitin mittal
的PHP
Java脚本
输出 :
5
该解决方案的时间复杂度为O(Log x)
相关文章
- 10-05 戈多的场景树
- 10-05 戈多动画
- 10-05 在 Godot 中设计标题画面
- 10-05 信息搜索和可视化
- 10-05 设计流程与任务分析
- 10-05 颤动警报对话框
- 10-05 PostgreSQL远程连接配置管理/账号密码分配
- 10-05 Windows server 创建FTP 包括ft
- 10-05 Mongodb副本集加分片集群安全认证使用账号密码
- 10-05 浅谈ubuntu中执行.sh文件的几种方式的区别
- 10-05 Linux性能优化的实用思路和技巧(linux性能
- 10-05 如何轻松安装Linux系统显卡驱动(Linux安装
- 10-05 win10动态锁设置教程
- 10-05 win10关闭Win10右下角提示的教程
- 10-05 win10设置定时提醒闹钟方法
- 10-05 win10音频服务未运行 错误1068怎么办
- 10-05 win10哪里下载
- 10-05 Win10命令提示符打不开怎么办
- 10-05 实现发送模板消息功能的微信小程序示例【通过open
- 10-05 微信公众平台发送模板消息(Java接口开发)
- 最近发表