Suma del equivalente decimal de todos los posibles pares de representación binaria de un número

Dado un número N. La tarea es encontrar la suma del equivalente decimal de todos los pares formados a partir de la representación binaria del número dado.

Ejemplos: 

Entrada : N = 4 
Salida : 4 
El equivalente binario de 4 es 100. 
Todos los pares posibles son 10, 10, 00 y su equivalente decimal es 2, 2, 0 respectivamente. 
Entonces, 2 + 2+ 0 = 4

Entrada : N = 11 
Salida : 13 
Todos los pares posibles son: 10, 11, 11, 01, 01, 11 
Suma = 2 + 3 + 3 + 1 + 1 + 3 = 13

Acercarse:  

  1. Encuentre el equivalente binario de N y guárdelo en un vector.
  2. Ejecute dos bucles para considerar todos y cada uno de los pares formados a partir de los bits de equivalente binario almacenados en el vector.
  3. Encuentra el equivalente decimal de todos los pares y súmalos.
  4. Devolver la suma.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the sum
int sumOfPairs(int n)
{
 
    // Store the Binary equivalent of decimal
    // number in reverse order
    vector<int> 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<Integer> v = new ArrayList<Integer>();
    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<int> v = new List<int>();
        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

<script>
 
// Javascript implementation of above approach
 
// Function to find the sum
function sumOfPairs(n)
{
     
    // Store the Binary equivalent of
    // decimal number in reverse order
    var v = [];
    var sum = 0;
 
    // Calculate binary equivalent
    // of decimal number
    while (n > 0)
    {
        v.push(n % 2);
        n = parseInt(n / 2);
    }
 
    // For correct binary representation
    v.reverse();
 
    // Consider every pair
    for(var i = 0; i < v.length - 1; i++)
    {
        for(var j = i + 1; j < v.length; 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
var N = 5;
document.write(sumOfPairs(N));
 
// This code is contributed by rrrtnx
 
</script>
Producción: 

6

 

Publicación traducida automáticamente

Artículo escrito por Shashank_Sharma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *