GrayCode.java
public class GrayCode
{
public static void yarg(String prefix, int
n)
{
if (n == 0) System.out.println(prefix);
else
{
gray(prefix + "1", n - 1);
yarg(prefix + "0", n - 1);
}
}
public static void gray(String prefix, int
n)
{
if (n == 0) System.out.println(prefix);
else
{
gray(prefix + "0", n - 1);
yarg(prefix + "1", n - 1);
}
}
public
static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
gray("", N);
}
}
No comments:
Post a Comment