C# - 如何创建一个只接收部分数组的函数?

时间:2017-11-05 15:16:27

标签: c#

我对编程非常陌生,我的好朋友给了我一些"作业"我可以解决。

他最近让我写了一个程序(控制台应用程序),它将写出alphabeth前N个字母的所有排列。所以如果N = 3,它会写:

ABC ACB BAC BCA 出租车 CBA

我已经了解了递归函数,数组和列表。这就是我到目前为止所做的,我从stackoverflow的几个答案中得到了它,我需要学习的是如何让它只写出前N个字母:

 class Program
    {
        static void Main()
        {
            string[] items = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            foreach (string[] permutation in Permutation.GetPermutations<string>(items))
            {
                Console.WriteLine(String.Join(", ", permutation));
            }
            Console.ReadKey();
        }

    }

    public class Permutation
    {

        public static IEnumerable<T[]> GetPermutations<T>(T[] items)
        {
            int[] work = new int[items.Length];
            for (int i = 0; i < work.Length; i++)
            {
                work[i] = i;
            }
            foreach (int[] index in GetIntPermutations(work, 0, work.Length))
            {
                T[] result = new T[index.Length];
                for (int i = 0; i < index.Length; i++) result[i] = items[index[i]];
                yield return result;
            }
        }

        public static IEnumerable<int[]> GetIntPermutations(int[] index, int offset, int len)
        {
            if (len == 1)
            {
                yield return index;
            }
            else if (len == 2)
            {
                yield return index;
                Swap(index, offset, offset + 1);
                yield return index;
                Swap(index, offset, offset + 1);
            }
            else
            {
                foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1))
                {
                    yield return result;
                }
                for (int i = 1; i < len; i++)
                {
                    Swap(index, offset, offset + i);
                    foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1))
                    {
                        yield return result;
                    }
                    Swap(index, offset, offset + i);
                }
            }
        }

        private static void Swap(int[] index, int offset1, int offset2)
        {
            int temp = index[offset1];
            index[offset1] = index[offset2];
            index[offset2] = temp;
        }


    }

1 个答案:

答案 0 :(得分:0)

只需更改N即可设置您想要涉及的字母数量。你也可以随机,所以它不会让你总是前N个字母。

public class Program
{
    public static void Main(string[] args)
    {
        int N = 5;
        string[] items = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        var take = items.Take(N);
        string str = "";
        foreach(string s in take)
        {
            str += s;
        }
        char[] charArry = str.ToCharArray();
        permute(charArry, 0, str.Length-1);
    }

    static void permute(char[] arry, int i, int n)
    {
        int j;
        if (i==n)
            Console.WriteLine(arry);
        else
        {
            for(j = i; j <=n; j++)
            {
                swap(ref arry[i],ref arry[j]);
                permute(arry,i+1,n);
                swap(ref arry[i], ref arry[j]);
            }
        }
    }

    static void swap(ref char a, ref char b)
    {
        char tmp;
        tmp = a;
        a=b;
        b = tmp;
    }
}