Representar un número como la suma de los números pseudobinarios mínimos posibles

Dado un número, debe representar este número como la suma del número mínimo de números pseudobinarios posibles . Se dice que un número es pseudobinario si su número decimal consta de solo dos dígitos (0 y 1). Ejemplo: 11,10,101 son todos números pseudobinarios.
Ejemplos:- 
 

Input : 44
Output : 11 11 11 11

Explanation : 44 can be represented as sum of 
minimum 4 psuedobinary numbers as 11+11+11+11  

Input : 31
Output : 11 10 10

Explanation : 31 can be represented as sum of
minimum 3 psuedobinary numbers as 11+10+10  

La idea para hacer esto es primero observar cuidadosamente que necesitamos calcular el número mínimo de números pseudobinarios posibles. Para hacer esto, encontramos un nuevo número m tal que si para un lugar en el número n dado, el dígito no es cero, entonces el dígito en ese lugar en m es 1, de lo contrario, cero. Por ejemplo, si n = 5102, entonces m será 1101. Luego imprimiremos este número m y restaremos m de n. Seguiremos repitiendo estos pasos hasta que n sea mayor que cero.
 

C++

// C++ program to represent a given
// number as sum of minimum possible
// psuedobinary numbers
#include<iostream>
using namespace std;
 
// function to represent a given
// number as sum of minimum possible
// psuedobinary numbers
void psuedoBinary(int n)
{
    // Repeat below steps until n > 0
    while (n > 0)
    {                
        // calculate m (A number that has same
        // number of digits as n, but has 1 in
        // place of non-zero digits 0 in place
        // of 0 digits)
        int temp = n, m = 0, p = 1;
        while (temp)
        {
            int rem = temp % 10;
            temp = temp / 10;
 
            if (rem != 0)
                m += p;
             
            p *= 10;
        }
         
        cout << m << " ";
 
        // subtract m from n
        n = n - m;
    }
}
 
// Driver code
int main()
{
    int n = 31;
 
    psuedoBinary(n);
 
    return 0;
}

Java

// Java program to represent a given
// number as sum of minimum possible
// psuedobinary numbers
 
import java.util.*;
import java.lang.*;
 
class GFG
{
    public static void psuedoBinary(int n)
    {
        // Repeat below steps until n > 0
        while (n != 0)
        {
            // calculate m (A number that has same
            // number of digits as n, but has 1 in
            // place of non-zero digits 0 in place
            // of 0 digits)
            int temp = n, m = 0, p = 1;
            while(temp != 0)
            {
                int rem = temp % 10;
                temp = temp / 10;
 
                if (rem != 0)
                    m += p;
 
                p *= 10;
            }
 
            System.out.print(m + " ");
             
            // subtract m from n
            n = n - m;
        }
        System.out.println(" ");
    }
 
// Driver code
public static void main(String[] args)
    {
        int n = 31;
        psuedoBinary(n);
    }
}
 
// This code is contributed by Mohit Gupta_OMG

Python3

# Python3 program to represent
# a given number as sum of
# minimum possible psuedobinary
# numbers
 
# function to represent a
# given number as sum of
# minimum possible
# psuedobinary numbers
def psuedoBinary(n):
     
    # Repeat below steps
    # until n > 0
    while (n > 0):
         
        # calculate m (A number
        # that has same number
        # of digits as n, but
        # has 1 in place of non-zero
        # digits 0 in place of 0 digits)
        temp = n;
        m = 0;
        p = 1;
        while (temp):
            rem = temp % 10;
            temp = int(temp / 10);
             
            if (rem != 0):
                m += p;
            p *= 10;
         
        print(m,end=" ");
         
        # subtract m from n
        n = n - m;
 
# Driver code
n = 31;
psuedoBinary(n);
 
# This code is contributed
# by mits.

C#

// C# program to represent a given
// number as sum of minimum possible
// psuedobinary numbers
 
using System;
 
class GFG
{
    public static void psuedoBinary(int n)
    {
        // Repeat below steps until n > 0
        while (n != 0)
        {
            // calculate m (A number that has same
            // number of digits as n, but has 1 in
            // place of non-zero digits 0 in place
            // of 0 digits)
            int temp = n, m = 0, p = 1;
            while(temp != 0)
            {
                int rem = temp % 10;
                temp = temp / 10;
 
                if (rem != 0)
                    m += p;
 
                p *= 10;
            }
 
            Console.Write(m + " ");
             
            // subtract m from n
            n = n - m;
        }
        Console.Write(" ");
    }
 
// Driver code
public static void Main()
    {
        int n = 31;
        psuedoBinary(n);
    }
}
 
// This code is contributed by nitin mittal

PHP

<?php
// PHP program to represent a
// given number as sum of minimum
// possible psuedobinary numbers
 
// Function to represent a
// given number as sum of minimum
// possible psuedobinary numbers
function psuedoBinary($n)
{
    // Repeat below steps until n > 0
    while ($n > 0)
    {                
        // calculate m (A number
        // that has same number of
        // digits as n, but has 1
        // in place of non-zero
        // digits 0 in place of 0
        // digits)
        $temp = $n; $m = 0; $p = 1;
        while ($temp)
        {
            $rem = $temp % 10;
            $temp = $temp / 10;
 
            if ($rem != 0)
                $m += $p;
             
            $p *= 10;
        }
         
        echo $m , " ";
 
        // subtract m from n
        $n = $n - $m;
    }
}
 
// Driver code
$n = 31;
psuedoBinary($n);
 
// This code is contributed
// by nitin mittal.
?>

Javascript

<script>
 
// JavaScript program to represent a given
// number as sum of minimum possible
// psuedobinary numbers   
 
function psuedoBinary( n)
{
        // Repeat below steps until n > 0
        while (n != 0)
        {
            // calculate m (A number that has same
            // number of digits as n, but has 1 in
            // place of non-zero digits 0 in place
            // of 0 digits)
            var temp = n, m = 0, p = 1;
            while (temp != 0) {
                var rem = temp % 10;
                temp = parseInt(temp / 10);
 
                if (rem != 0)
                    m += p;
 
                p *= 10;
            }
 
            document.write(m + " ");
 
            // subtract m from n
            n = n - m;
        }
        document.write(" ");
    }
 
    // Driver code
     
        var n = 31;
        psuedoBinary(n);
 
// This code is contributed by Amit Katiyar
 
</script>

Producción:  

11 10 10

Complejidad de tiempo : O (log n) 
Espacio auxiliar : O (1)
Este artículo es una contribución de Harsh Agarwal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 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 *