这是一个Java程序,用于将数字的二进制代码转换为等效的格雷码。
输入任何二进制数然后通过递归的方式得到格雷码作为输出。
import static java.lang.StrictMath.pow;
import java.util.Scanner;
public class Binary_Gray_Recursion
{
public static void main(String[] args)
{
int n, result = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter Binary number:");
n = s.nextInt();
Binary_Gray_Recursion obj = new Binary_Gray_Recursion();
result = obj.GrayCode(n, 0);
System.out.println("Gray Code:"+result);
}
int GrayCode(int x,int i)
{
int a, b, result = 0;
if(x != 0)
{
a = x % 10;
x = x / 10;
b = x % 10;
if((a & ~ b) == 1 || (~ a & b) == 1)
{
result = (int) (result + pow(10,i));
}
return GrayCode(x, ++i) + result;
}
return 0;
}
}
输出:
$ javac Binary_Gray_Recursion.java
$ java Binary_Gray_Recursion
Enter Binary number:1001
Gray Code:1101