📜  将数字表示为最小可能伪二进制数之和

📅  最后修改于: 2021-04-26 08:34:23             🧑  作者: Mango

给定一个数字,您必须将此数字表示为可能的伪二进制数的最小数目之和。如果数字的十进制数字仅由两位数字(0和1)组成,则称该数字为二进制数字。示例:11,10,101都是伪二进制数。

例子 :-

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  

这样做的想法是首先仔细观察我们需要计算可能的伪二进制数的最小数目。为此,我们找到一个新的数字m,如果对于给定数字n中的位置,该数字为非零,则m中该位置的数字为1,否则为零。例如,如果n = 5102,则m为1101。然后我们将打印此数字m,然后从n中减去m。我们将继续重复这些步骤,直到n大于零为止。

C++
// C++ program to represent a given 
// number as sum of minimum possible
// psuedobinary numbers
#include
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
 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.
?>


输出 :

11 10 10

时间复杂度:O(log n)
辅助空间:O(1)