原文:

给定一个数字 n,任务是找到只由 0 和 1 组成的所需数字,其和等于 n。 示例:

input: 9 
output: 1 1 1 1 1 1 1 1 1 
only numbers smaller than or equal to 9 with 
digits 0 and 1 only are 0 and 1 itself.
so to get 9, we have to add 1 - 9 times.
input: 31
output: 11 10 10

进场:

  1. 将产品 p 初始化为 1,将 m 初始化为零。
  2. 创建存储 0 和 1 的结果整数计数的向量。
  3. 循环 n 并检查 n 是否是 10 的倍数如果是,获取小数并通过乘以 10 来更新 p,并将该值存储在向量中,并将 n 减少 m。对每个小数执行此操作,并打印向量的总大小。
  4. 最后遍历向量并打印元素。

下面是上述方法的实现。

c

// c   implementation of the above approach
#include 
using namespace std;
// function to count the numbers
int findnumbers(int n)
{
    // initialize vector array that store
    // result.
    vector v;
    // get the each decimal and find its
    // count store in vector.
    while (n) {
        int n = n, m = 0, p = 1;
        while (n) {
            // find decimal
            if (n % 10)
              m  = p;
            n /= 10;
            p *= 10;
        }
        v.push_back(m);
        // decrement n by m for each decimal
        n -= m;
    }
    // loop for each element of vector
    // and print its content.
    for (int i = 0; i < v.size(); i  )
        cout << " " << v[i];
    return 0;
}
// driver code
int main()
{
    int n = 31;
    findnumbers(n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the above approach
import java.util.*;
public class gfg{
    // function to count the numbers
    public static int findnumbers(int n)
    {
        // initialize vector array that store
        // result.
        arraylist v = new arraylist();
        // get the each decimal and find its
        // count store in vector.
        while (n > 0) {
            int n = n, m = 0, p = 1;
            while (n > 0) {
                // find decimal
                if (n % 10 != 0)
                  m  = p;
                n /= 10;
                p *= 10;
            }
            v.add(m);
            // decrement n by m for each decimal
            n -= m;
        }
        // loop for each element of vector
        // and print its content.
        for (int i = 0; i < v.size(); i  )
            system.out.print(" "   v.get(i));
        return 0;
    }
     public static void main(string []args){
        int n = 31;
        findnumbers(n);
     }
}
// this code is contributed by rituraj jain

python 3

# python 3 implementation of
# the above approach
# function to count the numbers
def findnumbers(n) :
    # initialize vector array that
    # store result.
    v = [];
    # get the each decimal and find
    # its count store in vector.
    while (n) :
        n, m, p = n, 0, 1
        while (n) :
            # find decimal
            if (n % 10) :
                m  = p
            n //= 10
            p *= 10
        v.append(m);
        # decrement n by m for
        # each decimal
        n -= m
    # loop for each element of vector
    # and print its content.
    for i in range(len(v)) :
        print(v[i], end = " ")
# driver code
if __name__ == "__main__" :
    n = 31
    findnumbers(n)
# this code is contributed by ryuga

c

// c# implementation of the above approach
using system;
using system.collections;
class gfg
{
    // function to count the numbers
    public static int findnumbers(int n)
    {
        // initialize vector array that store
        // result.
        arraylist v = new arraylist();
        // get the each decimal and find its
        // count store in vector.
        while (n > 0)
        {
            int n = n, m = 0, p = 1;
            while (n > 0)
            {
                // find decimal
                if (n % 10 != 0)
                    m  = p;
                n /= 10;
                p *= 10;
            }
            v.add(m);
            // decrement n by m for each decimal
            n -= m;
        }
        // loop for each element of vector
        // and print its content.
        for (int i = 0; i < v.count; i  )
            console.write(" "   v[i]);
        return 0;
    }
    // driver code
    public static void main()
    {
        int n = 31;
        findnumbers(n);
    }
}
// this code is contributed by princiraj1992

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

11 10 10

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