给定数字N。任务是查找由给定数字的二进制表示形式形成的所有对的十进制等效值之和。
例子:
Input: N = 4
Output: 4
Binary equivalent of 4 is 100.
All possible pairs are 10, 10, 00 and their decimal equivalent are 2, 2, 0 respectively.
So, 2 + 2+ 0 = 4
Input: N = 11
Output: 13
All possible pairs are: 10, 11, 11, 01, 01, 11
Sum = 2 + 3 + 3 + 1 + 1 + 3 = 13
方法:
- 找出N的等价二进制并将其存储在向量中。
- 运行两个循环,以考虑由向量中存储的二进制等价位构成的每一对。
- 找到所有对的十进制等效项并将其相加。
- 返回总和。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to find the sum
int sumOfPairs(int n)
{
// Store the Binary equivalent of decimal
// number in reverse order
vector v;
int sum = 0;
// Calculate binary equivalent of decimal number
while (n > 0) {
v.push_back(n % 2);
n = n / 2;
}
// for correct binary representation
reverse(v.begin(), v.end());
// Consider every pair
for (int i = 0; i < v.size() - 1; i++) {
for (int j = i + 1; j < v.size(); j++)
{
// handles all combinations of 01
if (v[i] == 0 && v[j] == 1)
sum += 1;
// handles all combinations of 11
if (v[i] == 1 && v[j] == 1)
sum += 3;
// handles all combinations of 10
if (v[i] == 1 && v[j] == 0)
sum += 2;
}
}
return sum;
}
// Driver code
int main()
{
int N = 5;
cout << sumOfPairs(N);
return 0;
}
Java
// Java implementation of above approach
import java.util.*;
class GFG
{
public static int sumOfPairs(int n)
{
// Store the Binary equivalent
// of decimal number in reverse order
ArrayList v = new ArrayList();
int sum = 0;
// Calculate binary equivalent
// of decimal number
while (n > 0)
{
v.add(n % 2);
n = n / 2;
}
Collections.reverse(v);
for (int i = 0; i < v.size() - 1; i++)
{
for (int j = i + 1; j < v.size(); j++)
{
// handles all combinations of 01
if (v.get(i) == 0 && v.get(j) == 1)
sum += 1;
// handles all combinations of 11
if (v.get(i) == 1 && v.get(j) == 1)
sum += 3;
// handles all combinations of 10
if (v.get(i) == 1 && v.get(j) == 0)
sum += 2;
}
}
return sum;
}
// Driver Code
public static void main (String[] args)
{
int N = 5;
System.out.print(sumOfPairs(N));
}
}
// This code is contributed by Kirti_Mangal
Python3
# Python3 program to find the sum
# Function to find the sum
def sumofPairs(n) :
# Store the Binary equivalent of decimal
# number in reverse order
v = []
sum = 0
# Calculate binary equivalent of decimal number
while n > 0 :
v.append(n % 2)
n = n // 2
# for correct binary representation
v.reverse()
# Consider every pair
for i in range(len(v) - 1) :
for j in range(i + 1, len(v)) :
# handles all combinations of 01
if v[i] == 0 and v[j] == 1 :
sum += 1
# handles all combinations of 11
if v[i] == 1 and v[j] == 1 :
sum += 3
# handles all combinations of 10
if v[i] == 1 and v[j] == 0 :
sum += 2
return sum
# Driver Code
if __name__ == "__main__" :
N = 5
# function calling
print(sumofPairs(N))
# This code is contributed by ANKITRAI1
C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
class GFG
{
public static int sumOfPairs(int n)
{
// Store the Binary equivalent
// of decimal number in reverse order
List v = new List();
int sum = 0;
// Calculate binary equivalent
// of decimal number
while (n > 0)
{
v.Add(n % 2);
n = n / 2;
}
v.Reverse();
for (int i = 0; i < v.Count - 1; i++)
{
for (int j = i + 1; j < v.Count; j++)
{
// handles all combinations of 01
if (v[i] == 0 && v[j] == 1)
sum += 1;
// handles all combinations of 11
if (v[i] == 1 && v[j] == 1)
sum += 3;
// handles all combinations of 10
if (v[i] == 1 && v[j] == 0)
sum += 2;
}
}
return sum;
}
// Driver Code
public static void Main (String[] args)
{
int N = 5;
Console.WriteLine(sumOfPairs(N));
}
}
/* This code contributed by PrinciRaj1992 */
Javascript
输出:
6
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。