📌  相关文章
📜  生成具有相同长度的范围 [L, R] 中的所有二进制数

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

生成具有相同长度的范围 [L, R] 中的所有二进制数

给定两个正整数LR 。任务是将所有从 L 到 R 的数字转换为二进制数。所有二进制数的长度应该相同。

例子:

方法:按照下面提到的方法解决问题。

  • 要确定结果二进制数的长度,请取R+1的对数以 2 为底。
  • 然后从 L 遍历到 R并将每个数字转换为确定长度的二进制。
  • 存储每个数字并在最后打印。

下面是上述方法的实现

C++
// C++ code to implement the approach
#include 
using namespace std;
 
// Function to convert a number to binary
vector convertToBinary(int num,
                            int length)
{
    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 convert all numbers
// in range [L, R] to binary of
// same length
vector > getAllBinary(int l,
                                  int r)
{
 
    // Length of the binary numbers
    int n = (int) ceil(log(r+1) / log (2));
 
    vector > binary_nos;
 
    for (int i = l; i <= r; i++) {
        vector bits =
            convertToBinary(i, n);
        binary_nos.push_back(bits);
    }
 
    return binary_nos;
}
 
// Driver code
int main()
{
    int L = 2, R = 8;
    vector > binary_nos =
        getAllBinary(L, R);
    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 to implement the approach
import java.util.*;
public class GFG
{
 
  // Function to convert a number to binary
  static ArrayList convertToBinary(int num,
                                            int length)
  {
    ArrayList bits= new ArrayList();
    for(int i = 0; i < length; i++) {
      bits.add(0);
    }
    if (num == 0) {
      return bits;
    }
 
    int i = length - 1;
    while (num != 0) {
      bits.set(i, (num % 2));
      i = i - 1;
 
      // Integer division
      // gives quotient
      num = num / 2;
    }
 
    return bits;
  }
 
  // Function to convert all numbers
  // in range [L, R] to binary of
  // same length
  static ArrayList > getAllBinary(int l,
                                                     int r)
  {
 
    // Length of the binary numbers
    double x = Math.log(r+1);
    double y = Math.log (2);
    int n = (int) Math.ceil(x / y);
 
    ArrayList > binary_nos =
      new ArrayList >();
 
    for (int i = l; i <= r; i++) {
      ArrayList bits =
        convertToBinary(i, n);
      binary_nos.add(bits);
    }
 
    return binary_nos;
  }
 
  // Driver code
  public static void main(String args[])
  {
    int L = 2, R = 8;
    ArrayList > binary_nos =
      getAllBinary(L, R);
    for (int i = 0; i < binary_nos.size(); i++) {
      for (int j = 0; j < binary_nos.get(i).size(); j++) {
        System.out.print(binary_nos.get(i).get(j));
      }
      System.out.println();
    }
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python 3 code to implement the approach
import math
 
# Function to convert a number to binary
def convertToBinary(num, length):
 
    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 convert all numbers
# in range [L, R] to binary of
# same length
def getAllBinary(l, r):
 
    # Length of the binary numbers
    n = int(math.ceil(math.log(r+1)/ math.log(2)))
 
    binary_nos = []
 
    for i in range(l, r + 1):
        bits = convertToBinary(i, n)
        binary_nos.append(bits)
 
    return binary_nos
 
# Driver code
if __name__ == "__main__":
 
    L = 2
    R = 8
    binary_nos = getAllBinary(L, R)
    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 to implement the approach
using System;
using System.Collections.Generic;
public class GFG
{
 
  // Function to convert a number to binary
  static List convertToBinary(int num, int length)
  {
    List bits = new List();
    int i;
    for (i = 0; i < length; i++)
    {
      bits.Add(0);
    }
    if (num == 0)
    {
      return bits;
    }
 
    i = length - 1;
    while (num != 0)
    {
      bits[i] = (num % 2);
      i = i - 1;
 
      // Integer division
      // gives quotient
      num = num / 2;
    }
 
    return bits;
  }
 
  // Function to convert all numbers
  // in range [L, R] to binary of
  // same length
  static List> getAllBinary(int l, int r)
  {
 
    // Length of the binary numbers
    double x = Math.Log(r + 1);
    double y = Math.Log(2);
    int n = (int)Math.Ceiling(x / y);
 
    List> binary_nos = new List>();
 
    for (int i = l; i <= r; i++)
    {
      List bits = convertToBinary(i, n);
      binary_nos.Add(bits);
    }
 
    return binary_nos;
  }
 
  // Driver code
  public static void Main()
  {
    int L = 2, R = 8;
    List> binary_nos = getAllBinary(L, R);
    for (int i = 0; i < binary_nos.Count; i++)
    {
      for (int j = 0; j < binary_nos[i].Count; j++)
      {
        Console.Write(binary_nos[i][j]);
      }
      Console.WriteLine("");
    }
  }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript



输出
0010
0011
0100
0101
0110
0111
1000

时间复杂度: O(N * logR) 其中 N = (R – L + 1)
辅助空间: O(N * logR)