给定数字的二进制代码作为十进制数字,我们需要将其转换为等效的格雷码。
例子 :
Input : 1001
Output : 1101
Input : 11
Output : 10
在格雷码中,只有1位被2个连续的数字改变。
算法 :
binary_to_grey(n)
if n == 0
grey = 0;
else if last two bits are opposite to each other
grey = 1 + 10 * binary_to_gray(n/10))
else if last two bits are same
grey = 10 * binary_to_gray(n/10))
下面是上述方法的实现:
CPP
// CPP program to convert Binary to
// Gray code using recursion
#include
using namespace std;
// Function to change Binary to
// Gray using recursion
int binary_to_gray(int n)
{
if (!n)
return 0;
// Taking last digit
int a = n % 10;
// Taking second last digit
int b = (n / 10) % 10;
// If last digit are opposite bits
if ((a && !b) || (!a && b))
return (1 + 10 * binary_to_gray(n / 10));
// If last two bits are same
return (10 * binary_to_gray(n / 10));
}
// Driver Function
int main()
{
int binary_number = 1011101;
printf("%d", binary_to_gray(binary_number));
return 0;
}
Java
// Java program to convert
// Binary code to Gray code
import static java.lang.StrictMath.pow;
import java.util.Scanner;
class bin_gray
{
// Function to change Binary to
// Gray using recursion
int binary_to_gray(int n, int i)
{
int a, b;
int result = 0;
if (n != 0)
{
// Taking last digit
a = n % 10;
n = n / 10;
// Taking second last digit
b = n % 10;
if ((a & ~ b) == 1 || (~ a & b) == 1)
{
result = (int) (result + pow(10,i));
}
return binary_to_gray(n, ++i) + result;
}
return 0;
}
// Driver Function
public static void main(String[] args)
{
int binary_number;
int result = 0;
binary_number = 1011101;
bin_gray obj = new bin_gray();
result = obj.binary_to_gray(binary_number,0);
System.out.print(result);
}
}
// This article is contributed by Anshika Goyal.
Python3
# Python3 code to convert Binary
# to Gray code using recursion
# Function to change Binary to Gray using recursion
def binary_to_gray( n ):
if not(n):
return 0
# Taking last digit
a = n % 10
# Taking second last digit
b = int(n / 10) % 10
# If last digit are opposite bits
if (a and not(b)) or (not(a) and b):
return (1 + 10 * binary_to_gray(int(n / 10)))
# If last two bits are same
return (10 * binary_to_gray(int(n / 10)))
# Driver Code
binary_number = 1011101
print( binary_to_gray(binary_number), end='')
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to convert
// Binary code to Gray code
using System;
class GFG {
// Function to change Binary to
// Gray using recursion
static int binary_to_gray(int n, int i)
{
int a, b;
int result = 0;
if (n != 0)
{
// Taking last digit
a = n % 10;
n = n / 10;
// Taking second last digit
b = n % 10;
if ((a & ~ b) == 1 || (~ a & b) == 1)
{
result = (int) (result + Math.Pow(10,i));
}
return binary_to_gray(n, ++i) + result;
}
return 0;
}
// Driver Function
public static void Main()
{
int binary_number;
binary_number = 1011101;
Console.WriteLine(binary_to_gray(binary_number,0));
}
}
// This article is contributed by vt_m.
PHP
Javascript
输出 :
1110011
假定二进制数在整数范围内。对于更大的值,我们可以将二进制数作为字符串。