📜  表示以 -2 为底的数字 N

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

表示以 -2 为底的数字 N

给定一个整数N,任务是以字符串的形式找到数字N基数N 表示,使得S 0 * (- 2) 0 + S 1 * (- 2) 1 + ... + S k * (- 2) k = N 。该字符串应仅由01组成,除非字符串等于0,否则初始字符应为1

例子:

解决方法:按照以下步骤解决问题:

  • 初始化一个空字符串S
  • N 开始迭代,直到N大于零。
    • 如果N是偶数,则在S前面附加 ' 0 ' 并将N除以-2
    • 否则,在S前面附加 ' 1 ',然后将N1 ,然后将N除以-2
  • 如果字符串S为空,则返回 ' 0 '
  • 最后,返回字符串S。

下面是上述方法的实现:

C++
// C++ Program for above approach
#include 
using namespace std;
 
// Function to convert N to
// equivalent representation in base -2
string BaseConversion(int N)
{
 
    // Stores the required answer
    string s = "";
 
    // Iterate until N is
    // not equal to zero
    while (N != 0) {
 
        // If N is Even
        if (N % 2 == 0) {
 
            // Add char '0' in
            // front of string
            s = "0" + s;
        }
        else {
 
            // Add char '1' in
            // front of string
            s = "1" + s;
 
            // Decrement N by 1
            N--;
        }
 
        // Divide N by -2
        N /= -2;
    }
 
    // If string is empty,
    // that means N is zero
    if (s == "") {
 
        // Put '0' in string s
        s = "0";
    }
    return s;
}
 
// Driver Code
int main()
{
 
    // Given Input
    int N = -9;
 
    // Function Call
    cout << BaseConversion(N);
    return 0;
}


Java
// Java Program for above approach
class GFG {
    // Function to convert N to
    // equivalent representation in base -2
    public static String BaseConversion(int N) {
 
        // Stores the required answer
        String s = "";
 
        // Iterate until N is
        // not equal to zero
        while (N != 0) {
 
            // If N is Even
            if (N % 2 == 0) {
 
                // Add char '0' in
                // front of string
                s = "0" + s;
            } else {
 
                // Add char '1' in
                // front of string
                s = "1" + s;
 
                // Decrement N by 1
                N--;
            }
 
            // Divide N by -2
            N /= -2;
        }
 
        // If string is empty,
        // that means N is zero
        if (s == "") {
 
            // Put '0' in string s
            s = "0";
        }
        return s;
    }
 
    // Driver Code
    public static void main(String args[]) {
 
        // Given Input
        int N = -9;
 
        // Function Call
        System.out.println(BaseConversion(N));
    }
 
}
 
// This code is contributed by _saurabh_jaiswal.


Python3
# Python Program for the above approach
 
# Function to convert N to
# equivalent representation in base -2
def BaseConversion(N):
 
    # Stores the required answer
    s = ""
 
    # Iterate until N is
    # not equal to zero
    while (N != 0):
 
        # If N is Even
        if (N % 2 == 0):
 
            # Add char '0' in
            # front of string
            s = "0" + s
 
        else:
 
            # Add char '1' in
            # front of string
            s = "1" + s
 
            # Decrement N by 1
            N -= 1
 
        # Divide N by -2
        N /= -2
 
    # If string is empty,
    # that means N is zero
    if (s == ""):
 
        # Put '0' in string s
        s = "0"
 
    return s
 
 
# Driver Code
 
# Given Input
N = -9
 
# Function Call
print(BaseConversion(N))
 
# This code is contributed by _saurabh_jaiswal


C#
// C# Program for above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to convert N to
// equivalent representation in base -2
static string BaseConversion(int N)
{
 
    // Stores the required answer
    string s = "";
 
    // Iterate until N is
    // not equal to zero
    while (N != 0) {
 
        // If N is Even
        if (N % 2 == 0) {
 
            // Add char '0' in
            // front of string
            s = "0" + s;
        }
        else {
 
            // Add char '1' in
            // front of string
            s = "1" + s;
 
            // Decrement N by 1
            N--;
        }
 
        // Divide N by -2
        N /= -2;
    }
 
    // If string is empty,
    // that means N is zero
    if (s == "") {
 
        // Put '0' in string s
        s = "0";
    }
    return s;
}
 
// Driver Code
public static void Main()
{
 
    // Given Input
    int N = -9;
 
    // Function Call
    Console.Write(BaseConversion(N));
}
}
 
// This code is contributed by bgangwar59.


Javascript


输出:
1011

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