숫자 N 그리고 음수 염기 음수베이스 우리에게 주어진 것은 음의 기수로 n을 표현해야 한다는 것입니다. 음성 염기는 양성 염기와 유사하게 작동합니다. 예를 들어 2진수에서는 비트를 1 2 4 8 등으로 곱하여 실제 숫자를 10진수로 얻습니다. -2진법의 경우 10진수로 숫자를 얻으려면 비트에 1 -2 4 -8 등을 곱해야 합니다.
예:
파이썬 튜플 정렬
Input : n = 13 negBase = -2 Output : 11101 1*(16) + 1*(-8) + 1*(4) + 0*(-2) + 1*(1) = 13
동일한 절차를 사용하여 숫자를 음수로 표현하는 것이 가능합니다(참조 일주일 자세한 내용은). 단순화를 위해(출력에서 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.
따라서 음수 나머지를 얻으려면 밑수의 절대값을 더하고 몫에 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
JavaScript <script> // JavaScript program to convert n into // negative base form // Method to convert n to base negBase function toNegativeBase(n negBase) { // If n is zero then in any base // it will be 0 only if (n == 0) return '0'; let converted = '01'; while (n != 0) { // Get remainder by negative base // it can be negative also let remainder = (-1)*(Math.abs(n) % Math.abs(negBase)); n = parseInt(n/negBase); // if remainder is negative // add Math.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 = remainder.toString() + converted; } return converted; } // Driver Code let n = 13; let negBase = -2; document.write(toNegativeBase(n negBase)''); // This code is contributed by shinjanpatra </script>
산출:
11101
시간 복잡도: 에)
보조 공간: 오(1)
참조 :
https://en.wikipedia.org/wiki/Negative_base