给定整数N ,任务是找到数字N中存在的置位位数与数字N相反的绝对位数之间的绝对差。
例子:
Input: N = 13
Output: 2
Explanation:
Binary representation of (13)10 = (1101)2
Count of set bits = 3
Reverse of 13 is 31
Binary representation of (31)10 = (11111)2
Count of set bits of reversed number = 5
Absolute Difference is |3 – 5| =2
Input: N = 135
Output: 0
Explanation:
Binary representation of (135)10 = (10000111)2
Count of set bits =4
Reverse of 135 is 531
Binary representation of (531)10 = (1000010011)2
Count of set bits of reversed number = 4
Absolute Difference is |4 – 4| = 0
方法:主要思想是使用STL库的位集函数。
请按照以下步骤解决给定的问题:
- 反转数字N的数字并将其存储在变量中,例如revN 。
- 使用位设置函数计算N中的设置位数。
- 返回N和revN中设置位数的绝对差。
下面是上述方法的实现:
C++14
// C++ program for
// the above approach
#include
using namespace std;
// Function to find the
// reverse number of N
int reverse(int N)
{
// Stores the
// reverse of N
int revn = 0;
// Iterate while N exceeds 0
while (N > 0) {
// Extract last digit of N
int b = N % 10;
// Append the last digit
// of N to revn
revn = (revn * 10) + b;
// Remove the last digit of N
N = N / 10;
}
return revn;
}
// Function to find the absolute difference
// between the set bits in N and its reverse
int findAbsoluteDiffernce(int N)
{
// Store N as bitset
bitset<64> a(N);
// Stores the reverse of N
int revn = reverse(N);
// Stores revn as bitset
bitset<64> b(revn);
// Count set bits in N
int setBitsInN = a.count();
// Count set bits in revn
int setBitsInRevN = b.count();
// Return the absolute difference of
// set bits in N and its reverse
return abs(setBitsInN - setBitsInRevN);
}
// Driver Code
int main()
{
// Input
int N = 13;
// Function call to find absolute
// difference between the count
// of set bits in N and its reverse
cout << findAbsoluteDiffernce(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the
// reverse number of N
static int reverse(int N)
{
// Stores the
// reverse of N
int revn = 0;
// Iterate while N exceeds 0
while (N > 0)
{
// Extract last digit of N
int b = N % 10;
// Append the last digit
// of N to revn
revn = (revn * 10) + b;
// Remove the last digit of N
N = N / 10;
}
return revn;
}
// Function to find the absolute difference
// between the set bits in N and its reverse
static int findAbsoluteDiffernce(int N)
{
// Count set bits in N
int setBitsInN = Integer.bitCount(N);
// Stores the reverse of N
int revn = reverse(N);
// Count set bits in revn
int setBitsInRevN = Integer.bitCount(revn);
// Return the absolute difference of
// set bits in N and its reverse
return Math.abs(setBitsInN - setBitsInRevN);
}
// Driver Code
public static void main(String[] args)
{
// Input
int N = 13;
// Function call to find absolute
// difference between the count
// of set bits in N and its reverse
System.out.println(findAbsoluteDiffernce(N));
}
}
// This code is contributed by Kingash
Python3
# Python3 program for
# the above approach
# Function to find the
# reverse number of N
def reverse(N):
# Stores the
# reverse of N
revn = 0
# Iterate while N exceeds 0
while (N > 0):
# Extract last digit of N
b = N % 10
# Append the last digit
# of N to revn
revn = (revn * 10) + b
# Remove the last digit of N
N = N // 10
return revn
def countSetBits(n):
count = 0
while n:
count += (n & 1)
n >>= 1
return count
# Function to find the absolute difference
# between the set bits in N and its reverse
def findAbsoluteDiffernce(N):
# Count set bits in N
setBitsInN = countSetBits(N)
# Stores the reverse of N
revn = reverse(N)
# Count set bits in revn
setBitsInRevN = countSetBits(revn)
# Return the absolute difference of
# set bits in N and its reverse
return abs(setBitsInN - setBitsInRevN)
# Driver Code
# Input
N = 13
# Function call to find absolute
# difference between the count
# of set bits in N and its reverse
print(findAbsoluteDiffernce(N))
# This code is contributed by rohitsingh07052
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the
// reverse number of N
static int reverse(int N)
{
// Stores the
// reverse of N
int revn = 0;
// Iterate while N exceeds 0
while (N > 0)
{
// Extract last digit of N
int b = N % 10;
// Append the last digit
// of N to revn
revn = (revn * 10) + b;
// Remove the last digit of N
N = N / 10;
}
return revn;
}
// Function to get no of set
// bits in binary representation
// of positive integer n
static int countSetBits(int n)
{
int count = 0;
while (n > 0)
{
count += n & 1;
n >>= 1;
}
return count;
}
// Function to find the absolute difference
// between the set bits in N and its reverse
static int findAbsoluteDiffernce(int N)
{
// Count set bits in N
int setBitsInN = countSetBits(N);
// Stores the reverse of N
int revn = reverse(N);
// Count set bits in revn
int setBitsInRevN = countSetBits(revn);
// Return the absolute difference of
// set bits in N and its reverse
return Math.Abs(setBitsInN - setBitsInRevN);
}
// Driver Code
public static void Main(string[] args)
{
// Input
int N = 13;
// Function call to find absolute
// difference between the count
// of set bits in N and its reverse
Console.WriteLine(findAbsoluteDiffernce(N));
}
}
// This code is contributed by AnkThon
Javascript
输出:
2
时间复杂度: O(log N)
辅助空间: O(1)