📜  平衡三进制数系统

📅  最后修改于: 2021-04-22 02:31:36             🧑  作者: Mango

众所周知,二进制数系统是其中只有2位数字(即0和1 )的数字系统。同样,我们也知道,三进制数系统是其中只有3位数字(即0、1和2 )的数字系统。在本文中,我们将学习平衡三进制数系统
平衡三进制数系统是一种包括数字-1、0和1的数字系统。由于将-1写入数字很不方便,因此我们将进一步使用字母Z。
十进制到平衡三进制系统的转换
从十进制到平衡三进制的转换分两步完成:

  1. 将十进制转换为三进制数字系统。
  2. 使用以下步骤将三元转换为平衡三元系统:
    • 通过保留0和1来从右到左遍历三进制数
    • 遇到2时,将其更改为Z,然后在迭代中的下一位数字上加上+1。
    • 某些数字可能变为+3,然后将+3替换为0,并在迭代中将+1添加到下一个数字。
    • 完成此过程,直到您转换所有数字。

示例:将238 10转换为平衡三元,反之亦然

笔记:-
该系统还允许表示负数,从而消除了在数字之前使用负号的需要。三元平衡系统中的所有负数均以Z开头。
IE:
-1 10 = Z 3
−2 10 = Z1 3
−3 10 = Z0 3
−4 10 = ZZ 3
−5 10 = Z11 3

下面是将正小数转换为平衡三进制的程序:

C++
// C++ program to convert positive
// decimals into balanced ternary system
 
#include 
using namespace std;
 
string balancedTernary(int n)
{
    string output = "";
    while (n > 0) {
        int rem = n % 3;
        n = n / 3;
        if (rem == 2) {
            rem = -1;
            n++;
        }
        output = (rem == 0
                      ? '0'
                      : (rem == 1)
                            ? '1'
                            : 'Z')
                 + output;
    }
    return output;
}
 
// Driver code
int main()
{
 
    int n = 238;
 
    cout << "Equivalent Balanced Ternary of "
         << n << " is: "
         << balancedTernary(n);
 
    return 0;
}


Java
// Java program to convert positive
// decimals into balanced ternary system
class GFG{
 
static String balancedTernary(int n)
{
    String output = "";
    while (n > 0)
    {
        int rem = n % 3;
        n = n / 3;
        if (rem == 2)
        {
            rem = -1;
            n++;
        }
        output = (rem == 0 ? '0' :
                 (rem == 1) ? '1' : 'Z') + output;
    }
    return output;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 238;
 
    System.out.print("Equivalent Balanced Ternary of " +
                      n + " is: " + balancedTernary(n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to convert positive
# decimals into balanced ternary system
def balancedTernary(n):
 
    output = ""
     
    while(n > 0):
        rem = n % 3
        n = n // 3
         
        if(rem == 2):
            rem = -1
            n += 1
 
        if(rem == 0):
            output = '0' + output
        else:
            if(rem == 1):
                output = '1' + output
            else:
                output = 'Z' + output
 
    return output
 
# Driver Code
n = 238
 
# Function call
print("Equivalent Balanced Ternary of",
       n , "is:", balancedTernary(n))
 
# This code is contributed by Shivam Singh


C#
// C# program to convert positive
// decimals into balanced ternary system
using System;
using System.Collections.Generic;
class GFG{
 
static String balancedTernary(int n)
{
    String output = "";
    while (n > 0)
    {
        int rem = n % 3;
        n = n / 3;
        if (rem == 2)
        {
            rem = -1;
            n++;
        }
        output = (rem == 0 ? '0' :
                 (rem == 1) ? '1' : 'Z') + output;
    }
    return output;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 238;
 
    Console.Write("Equivalent Balanced Ternary of " +
                   n + " is: " + balancedTernary(n));
}
}
 
// This code is contributed by Rajput-Ji


输出:
Equivalent Balanced Ternary of 238 is: 100Z11