📌  相关文章
📜  将 [L, R] 范围内的所有数字转换为二进制数

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

将 [L, R] 范围内的所有数字转换为二进制数

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

例子:

方法:可以使用以下方法解决问题。

  • 从 L 遍历到 R 并将每个数字转换为二进制数。
  • 存储每个数字并在最后打印。

下面是上述方法的实现。

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to convert a number
// to its binary equivalent
vector convertToBinary(int num)
{
    vector bits;
    if (num == 0) {
        bits.push_back(0);
        return bits;
    }
 
    while (num != 0) {
        bits.push_back(num % 2);
 
        // Integer division
        // gives quotient
        num = num / 2;
    }
    reverse(bits.begin(), bits.end());
    return bits;
}
 
// Function to convert all numbers
// in range [L, R] to binary
vector > getAllBinary(int l,
                                  int r)
{
    // Vector to store the binary
    // representations of the numbers
    vector > binary_nos;
    for (int i = l; i <= r; i++) {
        vector bits =
            convertToBinary(i);
        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 above approach
import java.util.ArrayList;
import java.util.Collections;
 
class GFG {
 
  // Function to convert a number
  // to its binary equivalent
  public static ArrayList convertToBinary(int num) {
    ArrayList bits = new ArrayList();
    if (num == 0) {
      bits.add(0);
      return bits;
    }
 
    while (num != 0) {
      bits.add(num % 2);
 
      // Integer division
      // gives quotient
      num = num / 2;
    }
    Collections.reverse(bits);
    return bits;
  }
 
  // Function to convert all numbers
  // in range [L, R] to binary
  public static ArrayList> getAllBinary(int l, int r)
  {
 
    // Vector to store the binary
    // representations of the numbers
    ArrayList> binary_nos = new ArrayList>();
    for (int i = l; i <= r; i++)
    {
      ArrayList bits = convertToBinary(i);
      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 saurabh_jaiswal.


Python3
# Python 3 code to implement the above approach
 
# Function to convert a number
# to its binary equivalent
def convertToBinary(num):
 
    bits = []
    if (num == 0):
        bits.append(0)
        return bits
 
    while (num != 0):
        bits.append(num % 2)
 
        # Integer division
        # gives quotient
        num = num // 2
 
    bits.reverse()
    return bits
 
# Function to convert all numbers
# in range [L, R] to binary
def getAllBinary(l, r):
 
    # Vector to store the binary
    # representations of the numbers
    binary_nos = []
    for i in range(l, r+1):
        bits = convertToBinary(i)
        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 above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to convert a number
  // to its binary equivalent
  public static List convertToBinary(int num) {
    List bits = new List();
    if (num == 0) {
      bits.Add(0);
      return bits;
    }
 
    while (num != 0) {
      bits.Add(num % 2);
 
      // int division
      // gives quotient
      num = num / 2;
    }
    bits.Reverse();
    return bits;
  }
 
  // Function to convert all numbers
  // in range [L, R] to binary
  public static List> getAllBinary(int l, int r)
  {
 
    // List to store the binary
    // representations of the numbers
    List> binary_nos = new List>();
    for (int i = l; i <= r; i++)
    {
      List bits = convertToBinary(i);
      binary_nos.Add(bits);
    }
    return binary_nos;
  }
 
  // Driver code
  public static void Main(String []args)
  {
    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 29AjayKumar


Javascript



输出
10
11
100
101
110
111
1000

时间复杂度: O(N * LogR) 其中 N 是 [L, R] 范围内的数字计数
辅助空间: O(N * logR)