📌  相关文章
📜  生成所有N位的二进制字符串

📅  最后修改于: 2021-05-30 13:37:41             🧑  作者: Mango

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

例子:

Input: 2
Output:
0 0
0 1
1 0
1 1

Input: 3
Output:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

方法:想法是尝试所有排列。对于每个位置,都有2个选项,即“ 0”或“ 1”。回溯用于此方法以尝试各种可能性/排列。

下面是上述方法的实现:

C++
// C++ implementation of the above approach:
  
#include 
using namespace std;
  
// Function to print the output
void printTheArray(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}
  
// Function to generate all binary strings
void generateAllBinaryStrings(int n, int arr[], int i)
{
    if (i == n) {
        printTheArray(arr, n);
        return;
    }
  
    // First assign "0" at ith position
    // and try for all other permutations
    // for remaining positions
    arr[i] = 0;
    generateAllBinaryStrings(n, arr, i + 1);
  
    // And then assign "1" at ith position
    // and try for all other permutations
    // for remaining positions
    arr[i] = 1;
    generateAllBinaryStrings(n, arr, i + 1);
}
  
// Driver Code
int main()
{
    int n = 4;
  
    int arr[n];
  
    // Print all binary strings
    generateAllBinaryStrings(n, arr, 0);
  
    return 0;
}


Java
// Java implementation of the above approach:
import java.util.*;
  
class GFG
{
  
// Function to print the output
static void printTheArray(int arr[], int n)
{
    for (int i = 0; i < n; i++) 
    {
        System.out.print(arr[i]+" ");
    }
    System.out.println();
}
  
// Function to generate all binary strings
static void generateAllBinaryStrings(int n, 
                            int arr[], int i)
{
    if (i == n) 
    {
        printTheArray(arr, n);
        return;
    }
  
    // First assign "0" at ith position
    // and try for all other permutations
    // for remaining positions
    arr[i] = 0;
    generateAllBinaryStrings(n, arr, i + 1);
  
    // And then assign "1" at ith position
    // and try for all other permutations
    // for remaining positions
    arr[i] = 1;
    generateAllBinaryStrings(n, arr, i + 1);
}
  
// Driver Code
public static void main(String args[])
{
    int n = 4;
  
    int[] arr = new int[n];
  
    // Print all binary strings
    generateAllBinaryStrings(n, arr, 0);
}
}
  
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the 
# above approach 
  
# Function to print the output 
def printTheArray(arr, n): 
  
    for i in range(0, n): 
        print(arr[i], end = " ") 
      
    print()
  
# Function to generate all binary strings 
def generateAllBinaryStrings(n, arr, i): 
  
    if i == n:
        printTheArray(arr, n) 
        return
      
    # First assign "0" at ith position 
    # and try for all other permutations 
    # for remaining positions 
    arr[i] = 0
    generateAllBinaryStrings(n, arr, i + 1) 
  
    # And then assign "1" at ith position 
    # and try for all other permutations 
    # for remaining positions 
    arr[i] = 1
    generateAllBinaryStrings(n, arr, i + 1) 
  
# Driver Code 
if __name__ == "__main__": 
  
    n = 4
    arr = [None] * n 
  
    # Print all binary strings 
    generateAllBinaryStrings(n, arr, 0) 
  
# This code is contributed 
# by Rituraj Jain


C#
// C# implementation of the above approach:
using System;
  
class GFG
{
  
// Function to print the output
static void printTheArray(int []arr, int n)
{
    for (int i = 0; i < n; i++) 
    {
        Console.Write(arr[i]+" ");
    }
    Console.WriteLine();
}
  
// Function to generate all binary strings
static void generateAllBinaryStrings(int n, 
                            int []arr, int i)
{
    if (i == n) 
    {
        printTheArray(arr, n);
        return;
    }
  
    // First assign "0" at ith position
    // and try for all other permutations
    // for remaining positions
    arr[i] = 0;
    generateAllBinaryStrings(n, arr, i + 1);
  
    // And then assign "1" at ith position
    // and try for all other permutations
    // for remaining positions
    arr[i] = 1;
    generateAllBinaryStrings(n, arr, i + 1);
}
  
// Driver Code
public static void Main(String []args)
{
    int n = 4;
  
    int[] arr = new int[n];
  
    // Print all binary strings
    generateAllBinaryStrings(n, arr, 0);
}
}
  
// This code has been contributed by 29AjayKumar


PHP


输出:
0 0 0 0 
0 0 0 1 
0 0 1 0 
0 0 1 1 
0 1 0 0 
0 1 0 1 
0 1 1 0 
0 1 1 1 
1 0 0 0 
1 0 0 1 
1 0 1 0 
1 0 1 1 
1 1 0 0 
1 1 0 1 
1 1 1 0 
1 1 1 1

相关文章:生成所有从0到n的二进制数

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”