可以使用一组数字或符号将数字系统视为数字的数学符号。用简单的话来说,数字系统是一种表示数字的方法。每个数字系统都通过其基数或基数来识别。
例如,在微处理器编程中使用Binary , Octal , Decimal和Hexadecimal Number系统。在本文中,将讨论一个这样的数字系统。
三元数系统:
如果数字系统的基值为3,则此表示形式称为三进制表示形式。该系统中的数字为0,1和2 。
还有一个称为“平衡三进制”的数字系统,其中包括数字-1、0和+1 。数字的三进制表示比二进制数字系统紧凑。
将十进制转换为三进制的步骤:
- 将数字除以3。
- 获取下一次迭代的整数商。
- 获取三进制数的余数。
- 重复这些步骤,直到商等于0。
例如: N =101。下图说明了将101 10逐步转换为base-3的过程。
以下是十进制转二进制和反Versa的实现:
C++
// C++ program to convert decimal
// number to ternary number
#include
#include
#include
using namespace std;
// Function to convert a decimal
// number to a ternary number
void convertToTernary(int N)
{
// Base case
if (N == 0)
return;
// Finding the remainder
// when N is divided by 3
int x = N % 3;
N /= 3;
if (x < 0)
N += 1;
// Recursive function to
// call the function for
// the integer division
// of the value N/3
convertToTernary(N);
// Handling the negative cases
if (x < 0)
cout << x + (3 * -1);
else
cout << x;
}
// Function to convert the decimal to ternary
void convert(int Decimal)
{
cout << "Ternary number of "
<< Decimal << " is: ";
// If the number is greater
// than 0, compute the
// ternary representation
// of the number
if (Decimal != 0) {
convertToTernary(Decimal);
}
else
cout << "0" << endl;
}
// Driver code
int main()
{
int Decimal = 2747;
convert(Decimal);
return 0;
}
Java
// Java program to convert decimal
// number to ternary number
import java.io.*;
class GFG
{
// Function to convert a decimal
// number to a ternary number
static void convertToTernary(int N)
{
// Base case
if (N == 0)
return;
// Finding the remainder
// when N is divided by 3
int x = N % 3;
N /= 3;
if (x < 0)
N += 1;
// Recursive function to
// call the function for
// the integer division
// of the value N/3
convertToTernary(N);
// Handling the negative cases
if (x < 0)
System.out.print( x + (3 * -1));
else
System.out.print( x);
}
// Function to convert the decimal to ternary
static void convert(int Decimal)
{
System.out.print("Ternary number of " +Decimal +" is: ");
// If the number is greater
// than 0, compute the
// ternary representation
// of the number
if (Decimal != 0) {
convertToTernary(Decimal);
}
else
System.out.println("0" );
}
// Driver Code
public static void main (String[] args)
{
int Decimal = 2747;
convert(Decimal);
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program to convert decimal
# number to ternary number
# Function to convert a decimal
# number to a ternary number
def convertToTernary(N):
# Base case
if (N == 0):
return;
# Finding the remainder
# when N is divided by 3
x = N % 3;
N //= 3;
if (x < 0):
N += 1;
# Recursive function to
# call the function for
# the integer division
# of the value N/3
convertToTernary(N);
# Handling the negative cases
if (x < 0):
print(x + (3 * -1), end = "");
else:
print(x, end = "");
# Function to convert the
# decimal to ternary
def convert(Decimal):
print("Ternary number of ", Decimal,
" is: ", end = "");
# If the number is greater
# than 0, compute the
# ternary representation
# of the number
if (Decimal != 0):
convertToTernary(Decimal);
else:
print("0", end = "");
# Driver Code
if __name__ == '__main__':
Decimal = 2747;
convert(Decimal);
# This code is contributed by Rajput-Ji
C#
// C# program to convert decimal
// number to ternary number
using System;
class GFG
{
// Function to convert a decimal
// number to a ternary number
static void convertToTernary(int N)
{
// Base case
if (N == 0)
return;
// Finding the remainder
// when N is divided by 3
int x = N % 3;
N /= 3;
if (x < 0)
N += 1;
// Recursive function to
// call the function for
// the integer division
// of the value N/3
convertToTernary(N);
// Handling the negative cases
if (x < 0)
Console.Write( x + (3 * -1));
else
Console.Write( x);
}
// Function to convert the decimal to ternary
static void convert(int Decimal)
{
Console.Write("Ternary number of " +Decimal +" is: ");
// If the number is greater
// than 0, compute the
// ternary representation
// of the number
if (Decimal != 0) {
convertToTernary(Decimal);
}
else
Console.WriteLine("0" );
}
// Driver Code
public static void Main (string[] args)
{
int Decimal = 2747;
convert(Decimal);
}
}
// This code is contributed by AnkitRai01
Javascript
C++
// C++ program to convert a
// ternary number to decimal number
#include
#include
#include
using namespace std;
// Function to convert a ternary
// number to a decimal number
void convertToDecimal(int N)
{
cout << "Decimal number of "
<< N << " is: ";
// If the number is greater than 0,
// compute the decimal
// representation of the number
if (N != 0) {
int decimalNumber = 0,
i = 0, remainder;
// Loop to iterate through
// the number
while (N != 0) {
remainder = N % 10;
N /= 10;
// Computing the decimal digit
decimalNumber += remainder
* pow(3, i);
++i;
}
cout << decimalNumber << endl;
}
else
cout << "0" << endl;
}
// Driver code
int main()
{
int Ternary = 10202202;
convertToDecimal(Ternary);
return 0;
}
Java
// Java program to convert a
// ternary number to decimal number
class GFG{
// Function to convert a ternary
// number to a decimal number
static void convertToDecimal(int N)
{
System.out.print("Decimal number of " +
N + " is: ");
// If the number is greater than 0,
// compute the decimal
// representation of the number
if (N != 0)
{
int decimalNumber = 0,
i = 0, remainder;
// Loop to iterate through
// the number
while (N != 0)
{
remainder = N % 10;
N /= 10;
// Computing the decimal digit
decimalNumber += remainder *
Math.pow(3, i);
++i;
}
System.out.print(decimalNumber + "\n");
}
else
System.out.print("0" + "\n");
}
// Driver code
public static void main(String[] args)
{
int Ternary = 10202202;
convertToDecimal(Ternary);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to convert a
# ternary number to decimal number
import math;
# Function to convert a ternary
# number to a decimal number
def convertToDecimal(N):
print("Decimal number of", N, "is:", end = " ");
# If the number is greater than 0,
# compute the decimal
# representation of the number
if (N != 0):
decimalNumber = 0;
i = 0;
remainder = 0;
# Loop to iterate through
# the number
while (N != 0):
remainder = N % 10;
N = N // 10;
# Computing the decimal digit
decimalNumber += remainder * math.pow(3, i);
i += 1;
print(decimalNumber);
else:
print("0");
# Driver code
Ternary = 10202202;
convertToDecimal(Ternary);
# This code is contributed by Code_Mech
C#
// C# program to convert a ternary
// number to decimal number
using System;
class GFG{
// Function to convert a ternary
// number to a decimal number
static void convertToDecimal(int N)
{
Console.Write("Decimal number of " +
N + " is: ");
// If the number is greater than
// 0, compute the decimal
// representation of the number
if (N != 0)
{
int decimalNumber = 0;
int i = 0, remainder;
// Loop to iterate through
// the number
while (N != 0)
{
remainder = N % 10;
N /= 10;
// Computing the decimal digit
decimalNumber += remainder *
(int)Math.Pow(3, i);
++i;
}
Console.Write(decimalNumber + "\n");
}
else
Console.Write("0" + "\n");
}
// Driver code
public static void Main()
{
int Ternary = 10202202;
convertToDecimal(Ternary);
}
}
// This code is contributed by shivanisinghss2110
输出:
Ternary number of 2747 is: 10202202
将三进制转换为十进制的步骤:
- 将三进制数中的每个数字与其对应的3的幂连接起来。
- 将每个数字乘以其相应的三的幂,然后将所有得到的数字相加。
例如: N =10202。下图说明了10202 3到10的逐步转换。
以下是十进制转二进制和反Versa的实现:
C++
// C++ program to convert a
// ternary number to decimal number
#include
#include
#include
using namespace std;
// Function to convert a ternary
// number to a decimal number
void convertToDecimal(int N)
{
cout << "Decimal number of "
<< N << " is: ";
// If the number is greater than 0,
// compute the decimal
// representation of the number
if (N != 0) {
int decimalNumber = 0,
i = 0, remainder;
// Loop to iterate through
// the number
while (N != 0) {
remainder = N % 10;
N /= 10;
// Computing the decimal digit
decimalNumber += remainder
* pow(3, i);
++i;
}
cout << decimalNumber << endl;
}
else
cout << "0" << endl;
}
// Driver code
int main()
{
int Ternary = 10202202;
convertToDecimal(Ternary);
return 0;
}
Java
// Java program to convert a
// ternary number to decimal number
class GFG{
// Function to convert a ternary
// number to a decimal number
static void convertToDecimal(int N)
{
System.out.print("Decimal number of " +
N + " is: ");
// If the number is greater than 0,
// compute the decimal
// representation of the number
if (N != 0)
{
int decimalNumber = 0,
i = 0, remainder;
// Loop to iterate through
// the number
while (N != 0)
{
remainder = N % 10;
N /= 10;
// Computing the decimal digit
decimalNumber += remainder *
Math.pow(3, i);
++i;
}
System.out.print(decimalNumber + "\n");
}
else
System.out.print("0" + "\n");
}
// Driver code
public static void main(String[] args)
{
int Ternary = 10202202;
convertToDecimal(Ternary);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to convert a
# ternary number to decimal number
import math;
# Function to convert a ternary
# number to a decimal number
def convertToDecimal(N):
print("Decimal number of", N, "is:", end = " ");
# If the number is greater than 0,
# compute the decimal
# representation of the number
if (N != 0):
decimalNumber = 0;
i = 0;
remainder = 0;
# Loop to iterate through
# the number
while (N != 0):
remainder = N % 10;
N = N // 10;
# Computing the decimal digit
decimalNumber += remainder * math.pow(3, i);
i += 1;
print(decimalNumber);
else:
print("0");
# Driver code
Ternary = 10202202;
convertToDecimal(Ternary);
# This code is contributed by Code_Mech
C#
// C# program to convert a ternary
// number to decimal number
using System;
class GFG{
// Function to convert a ternary
// number to a decimal number
static void convertToDecimal(int N)
{
Console.Write("Decimal number of " +
N + " is: ");
// If the number is greater than
// 0, compute the decimal
// representation of the number
if (N != 0)
{
int decimalNumber = 0;
int i = 0, remainder;
// Loop to iterate through
// the number
while (N != 0)
{
remainder = N % 10;
N /= 10;
// Computing the decimal digit
decimalNumber += remainder *
(int)Math.Pow(3, i);
++i;
}
Console.Write(decimalNumber + "\n");
}
else
Console.Write("0" + "\n");
}
// Driver code
public static void Main()
{
int Ternary = 10202202;
convertToDecimal(Ternary);
}
}
// This code is contributed by shivanisinghss2110
输出:
Decimal number of 10202202 is: 2747