📌  相关文章
📜  将数字转换为负基数表示

📅  最后修改于: 2021-04-27 18:33:42             🧑  作者: Mango

给我们一个数字n和一个负数negBase ,我们需要在该负数中表示n。负基的工作原理类似于正基。例如,以2为底,我们将位乘以1、2、4、8,依此类推,以十进制形式获得实际数字。在以-2为底数的情况下,我们需要将位乘以1,-2、4,-8等,以十进制数表示数字。

例子:

Input  : n = 13, negBase = -2
Output : 11101
1*(16) + 1*(-8) + 1*(4) + 0*(-2) + 1*(1)  = 13

可以使用相同的步骤将数字表示为任何负数基数(有关详细信息,请参阅Wiki)。为了简单起见(为了消除输出中的A,B等字符),我们只允许基数介于-2和-10之间。

我们可以解决这个问题,类似于解决带有正数基数的问题,但是要记住的一件重要事情是,无论我们使用正数基数还是负数基数,余数始终是正数,但是在大多数编译器中,将负数除以负数的结果朝0舍入,通常剩下负数。
因此,只要我们得到负的余数,就可以将其转换为正数,如下所示:

Let 
n = (?negBase) * quotient + remainder 
  = (?negBase) * quotient + negBase ? negBase + negBase 
  = (?negBase) * (quotient + 1) + (remainder + negBase). 

So if after doing "remainder = n % negBase" and 
"n = n/negBase", we get negative remainder, we do 
following.
remainder = remainder + (-negBase)
n = n + 1

Example : n = -4, negBase = -3
In C++, we get
    remainder = n % negBase = -4/-3 = -1
    n = n/negBase [Next step for base conversion]
      = -4/-3 
      = 1
To avoid negative remainder, we do,
    remainder = -1 + (-negBase) = -1 - (-3) = 2
    n = n + 1 = 1  + 1 = 2.

因此,当我们得到负的余数时,可以通过将base的绝对值加上它的商加1来使其为正。

上面说明的方法是在下面的代码中实现的,

C++
//  C/C++ program to convert n into negative base form
#include 
using namespace std;
  
//  Utility method to convert integer into string
string toString(int n)
{
    string str;
    stringstream ss;
    ss << n;
    ss >> str;
    return str;
}
  
// Method to convert n to base negBase
string toNegativeBase(int n, int negBase)
{
    //  If n is zero then in any base it will be 0 only
    if (n == 0)
        return "0";
  
    string converted = "";
    while (n != 0)
    {
        // Get remainder by negative base, it can be
        // negative also
        int remainder = n % negBase;
        n /= negBase;
  
        // if remainder is negative, add abs(base) to
        // it and add 1 to n
        if (remainder < 0)
        {
            remainder += (-negBase);
            n += 1;
        }
  
        // convert remainder to string add into the result
        converted = toString(remainder) + converted;
    }
  
    return converted;
}
  
//  Driver code to test above methods
int main()
{
    int n = 13;
    int negBase = -2;
  
    cout << toNegativeBase(n, negBase);
  
    return 0;
}


Java
// Java program to convert n into 
// negative base form
class GFG
{
  
// Method to convert n to base negBase
static String toNegativeBase(int n, int negBase)
{
    // If n is zero then in any base
    // it will be 0 only
    if (n == 0)
        return "0";
  
    String converted = "";
    while (n != 0)
    {
        // Get remainder by negative base, 
        // it can be negative also
        int remainder = n % negBase;
        n /= negBase;
  
        // if remainder is negative, 
        // add Math.abs(base) to it 
        // and add 1 to n
        if (remainder < 0)
        {
            remainder += (-negBase);
            n += 1;
        }
  
        // convert remainder to String add into the result
        converted = String.valueOf(remainder) + converted;
    }
    return converted;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 13;
    int negBase = -2;
  
    System.out.print(toNegativeBase(n, negBase));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to convert n into 
# negative base form
  
# Method to convert n to base negBase
def toNegativeBase(n, negBase):
      
    # If n is zero then in any base it 
    # will be 0 only
    if (n == 0):
        return "0"
  
    converted = "01"
    while (n != 0):
          
        # Get remainder by negative base, 
        # it can be negative also
        remainder = n % (negBase)
        n = int(n/negBase)
  
        # if remainder is negative, add 
        # abs(base) to it and add 1 to n
        if (remainder < 0):
            remainder += ((-1) * negBase)
            n += 1
  
        # convert remainder to string add
        # into the result
        converted = str(remainder) + converted
          
    return converted
  
# Driver Code
if __name__ == '__main__':
    n = 13
    negBase = -2
  
    print(toNegativeBase(n, negBase))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to convert n into 
// negative base form
using System;
  
class GFG
{
  
// Method to convert n to base negBase
static String toNegativeBase(int n, int negBase)
{
    // If n is zero then in any base
    // it will be 0 only
    if (n == 0)
        return "0";
  
    String converted = "";
    while (n != 0)
    {
        // Get remainder by negative base, 
        // it can be negative also
        int remainder = n % negBase;
        n /= negBase;
  
        // if remainder is negative, 
        // add Math.Abs(base) to it 
        // and add 1 to n
        if (remainder < 0)
        {
            remainder += (-negBase);
            n += 1;
        }
  
        // convert remainder to String add into the result
        converted = String.Join("", remainder) + converted;
    }
    return converted;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 13;
    int negBase = -2;
  
    Console.Write(toNegativeBase(n, negBase));
}
}
  
// This code is contributed by Rajput-Ji


输出:

11101

参考 :
https://en.wikipedia.org/wiki/Negative_base