Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- /*
- public static int[] GrayCode(int k)
- {
- int[] result = {1};
- for(int n = 1; n < k; n++)
- {
- int[] newres = new int[2*result.Length + 1];
- int i = 0;
- for(i = 0; i < result.Length; i++)
- newres[i] = result[i];
- newres[i] = n+1; i++;
- for(int j = i; j <= 2*result.Length; j++)
- newres[j] = result[j-i];
- result = newres;
- }
- return result;
- }
- */
- public static int[] GrayCode(int k){
- List<int> list = new List<int>();
- GrayCode(0, k, list);
- return list.ToArray();
- }
- public static void GrayCode(int n, int k, List<int> list){
- if (k == n){
- return;
- }
- GrayCode(n, k - 1, list);
- list.Add(k);
- GrayCode(n, k - 1, list);
- }
- public static void Main()
- {
- Console.WriteLine(string.Join("", GrayCode(5)));
- /*
- for (int i = 1; i < 7; i++){
- //Console.WriteLine(string.Join("", GrayCode(i)));
- }
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement