📜  N位二进制数

📅  最后修改于: 2022-05-13 01:56:05.277000             🧑  作者: Mango

N位二进制数

给定一个正整数N 。任务是生成所有N 位的二进制数。这些二进制数应按升序排列

例子:

方法:对于任何数字长度N,将有2 N二进制数。

  • 因此从0 遍历到 2 N并将每个数字转换为二进制。
  • 存储每个数字并在最后打印。

下面是上述方法的实现。

C++
// C++ code to implement above approach
#include 
using namespace std;
 
// Function to convert number
// to binary of N bits
vector convertToBinary(int num,
                            int length)
{
    // Vector to store the number
    vector bits(length, 0);
    if (num == 0) {
        return bits;
    }
 
    int i = length - 1;
    while (num != 0) {
        bits[i--] = (num % 2);
 
        // Integer division
        // gives quotient
        num = num / 2;
    }
    return bits;
}
 
// Function to generate all
// N bit binary numbers
vector > getAllBinary(int n)
{
    vector > binary_nos;
 
    // Loop to generate the binary numbers
    for (int i = 0; i < pow(2, n); i++) {
        vector bits =
            convertToBinary(i, n);
        binary_nos.push_back(bits);
    }
 
    return binary_nos;
}
 
// Driver code
int main()
{
    int N = 3;
    vector > binary_nos =
        getAllBinary(N);
    for (int i = 0; i < binary_nos.size();
           i++) {
        for (int j = 0;
                j < binary_nos[i].size(); j++)
            cout << binary_nos[i][j];
        cout << endl;
    }
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
 
class GFG {
 
  // Function to convert number
  // to binary of N bits
  static int[] convertToBinary(int num,
                               int length)
  {
 
    // Vector to store the number
    int[] bits = new int[length];
    if (num == 0) {
      return bits;
    }
 
    int i = length - 1;
    while (num != 0) {
      bits[i--] = (num % 2);
 
      // Integer division
      // gives quotient
      num = num / 2;
    }
    return bits;
  }
 
  // Function to generate all
  // N bit binary numbers
  static int[][] getAllBinary(int n)
  {
    int[][] binary_nos = new int[(int)Math.pow(2,n)][];
    int k = 0;
     
    // Loop to generate the binary numbers
    for (int i = 0; i < Math.pow(2, n); i++) {
      int[] bits = convertToBinary(i, n);
      binary_nos[k++]= bits;
    }
 
    return binary_nos;
  }
 
  // Driver code
  public static void main (String[] args)
  {
    int N = 3;
    int[][] binary_nos = getAllBinary(N);
    for (int i = 0; i < binary_nos.length; i++) {
      for (int j = 0; j < binary_nos[i].length; j++)
        System.out.print(binary_nos[i][j]);
      System.out.println();
    }
 
  }
};
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 code to implement above approach
 
# Function to convert number
# to binary of N bits
def convertToBinary(num,
                    length):
 
    # Vector to store the number
    bits = [0]*(length)
    if (num == 0):
        return bits
 
    i = length - 1
    while (num != 0):
        bits[i] = (num % 2)
        i -= 1
 
        # Integer division
        # gives quotient
        num = num // 2
 
    return bits
 
# Function to generate all
# N bit binary numbers
def getAllBinary(n):
 
    binary_nos = []
 
    # Loop to generate the binary numbers
    for i in range(pow(2, n)):
        bits = convertToBinary(i, n)
        binary_nos.append(bits)
 
    return binary_nos
 
# Driver code
if __name__ == "__main__":
 
    N = 3
    binary_nos = getAllBinary(N)
    for i in range(len(binary_nos)):
        for j in range(len(binary_nos[i])):
            print(binary_nos[i][j], end="")
        print()
 
        # This code is contributed by ukasp.


C#
// C# code for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
 
  // Function to convert number
  // to binary of N bits
  static List convertToBinary(int num, int length) {
 
    // List to store the number
    List bits = new List();
    if (num == 0) {
      bits.Add(0);
      bits.Add(0);
      bits.Add(0);
      return bits;
    }
 
    int i = length - 1;
    while (num != 0) {
      bits.Add(num % 2);
 
      // int division
      // gives quotient
      num = num / 2;
    }
    while(bits.Count<3)
      bits.Add(0);
    return bits;
  }
 
  // Function to generate all
  // N bit binary numbers
  static List> getAllBinary(int n) {
    List> binary_nos = new List>();
 
    // Loop to generate the binary numbers
    for (int i = 0; i < Math.Pow(2, n); i++) {
      List bits = convertToBinary(i, n);
      binary_nos.Add(bits);
    }
 
    return binary_nos;
  }
 
  // Driver code
  public static void Main(String[] args) {
    int N = 3;
    List> binary_nos = getAllBinary(N);
    foreach(var st in binary_nos){
      foreach(var s in st){
        Console.Write(s);
      }
      Console.WriteLine();
    }
  }
}
 
// This code is contributed by Rajput-Ji


Javascript



输出
000
001
010
011
100
101
110
111

时间复杂度: O(2 N )
辅助空间: O(2 N )