C#  1到10的数组每次取三个不能重复,有多少种组合?{1,2,3,4,5,6,7,8,9,10}
比如1,2,3    1,2,4    1,2,5 等求算法输出

解决方案 »

  1.   

    组合问题(从M个不同字符中任取N个字符的所有组合)思路:状态数组,其下标表示1到m个数,数组元素的值为1,表示其下标代表的字符被选中,为0则没选中。 
    首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。 
    然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为“01”组合,同时将其左边的所有“1”全部移动到数组的最左端。 
    当“1”全部移动到最右端时,就得到了最后一个组合。 public String[] GetAllCombination(String str,Int32 n)  //n < str.Length 
            {
                Int32[] state = new Int32[str.Length];
                Char[] chars = str.ToCharArray();
                List<String> list = new List<String>();            for (Int32 i = 0; i < n; i++)
                {
                    state[i] = 1;
                }
                for (Int32 i = n; i < state.Length; i++)
                {
                    state[i] = 0;
                }            list.Add(str.Substring(0,n));
                while (true)
                {
                    Boolean flag = true;
                    Int32 index = 0;
                    Int32 oneCount = 0;
                    Int32 zeroCount = 0;
                    StringBuilder sbuilder = new StringBuilder();                for(; index < state.Length - 1; index++)
                    {
                        if (state[index] == 1 && state[index + 1] == 0)
                        {
                            Int32 temp = state[index];
                            state[index] = state[index + 1];
                            state[index + 1] = temp;
                            break;
                        }
                    }                for (Int32 i = 0; i < index; i++)
                    {
                        if (state[i] == 0)
                            zeroCount++;
                        else
                            oneCount++;
                    }                for (Int32 i = 0; i < index; i++)
                    {
                        if (i < oneCount)
                            state[i] = 1;
                        else
                            state[i] = 0;
                    }                for (Int32 i = 0; i < chars.Length; i++) 
                    {
                        if (state[i] == 1)
                            sbuilder.Append(chars[i]);
                    }                list.Add(sbuilder.ToString());                for (Int32 i = 0; i < chars.Length - n; i++)
                    {
                        if (state[i] == 1)
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                        break;
                }            return list.ToArray();
            }
    我的是输入一个字符串,和一个数字,比如 "abcde" 3, 然后会输出3个字母的全部组合,你稍微修改下就可以了from my blog
    http://www.cnblogs.com/Peter-Zhang/articles/1783814.html
      

  2.   

    Enumeralbe.Range
    var r = from a in Enumerable.Range(1, 10)
                        from b in Enumerable.Range(1,10)
                        from c in Enumerable.Range(1,10)
    http://topic.csdn.net/u/20090217/21/F41ED9F6-F929-451C-A5C9-80D2E408422A.html
      

  3.   

    class Program
        {
            static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            static void Main(string[] args)
            {
                int num = 0;
                string nunstr = "";
                for (int i = 0; i < numlist.Length-2; i++)
                {
                    for (int j = i + 1; j < numlist.Length - 1; j++)
                    {
                        nunstr = numlist[i] + "," + numlist[j] + "," + numlist[j + 1];
                        num++;
                        Console.WriteLine(nunstr);
                    }
                }
                Console.WriteLine(num.ToString());
            }
        }
      

  4.   

    少了一个循环
    static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            static void Main(string[] args)
            {
                int num = 0;
                string nunstr = "";
                for (int i = 0; i < numlist.Length-2; i++)
                {
                    for (int j = i + 1; j < numlist.Length - 1; j++)
                    {
                        for (int k = i + 2; k < numlist.Length; k++)
                        {
                            nunstr = numlist[i] + "," + numlist[j] + "," + numlist[k];
                            num++;
                            Console.WriteLine(nunstr);
                        }
                    }
                }
                Console.WriteLine(num.ToString());
            }
      

  5.   


    public static String[] GetAllCombination(Int32[] arr, Int32 n)  //n < str.Length 
            {
                Int32[] state = new Int32[arr.Length];
                List<String> list = new List<String>();            for (Int32 i = 0; i < n; i++)
                {
                    state[i] = 1;
                }
                for (Int32 i = n; i < state.Length; i++)
                {
                    state[i] = 0;
                }            String str = "";
                for(Int32 i = 0;i < n;i++)
                    str += arr[i] + ",";            list.Add(str.TrimEnd(','));            while (true)
                {
                    Boolean flag = true;
                    Int32 index = 0;
                    Int32 oneCount = 0;
                    Int32 zeroCount = 0;
                    StringBuilder sbuilder = new StringBuilder();                for (; index < state.Length - 1; index++)
                    {
                        if (state[index] == 1 && state[index + 1] == 0)
                        {
                            Int32 temp = state[index];
                            state[index] = state[index + 1];
                            state[index + 1] = temp;
                            break;
                        }
                    }                for (Int32 i = 0; i < index; i++)
                    {
                        if (state[i] == 0)
                            zeroCount++;
                        else
                            oneCount++;
                    }                for (Int32 i = 0; i < index; i++)
                    {
                        if (i < oneCount)
                            state[i] = 1;
                        else
                            state[i] = 0;
                    }                for (Int32 i = 0; i < arr.Length; i++)
                    {
                        if (state[i] == 1)
                            sbuilder.Append(arr[i] + ",");
                    }                list.Add(sbuilder.ToString().TrimEnd(','));                for (Int32 i = 0; i < arr.Length - n; i++)
                    {
                        if (state[i] == 1)
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                        break;
                }            return list.ToArray();
            }
    这回可以了
      

  6.   


    static void Main(string[] args)
    {
        List<int> arr = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
        StringBuilder builder = new StringBuilder();
        arr.ForEach(a1 => arr.Except(new int[] { a1 }).ToList().ForEach(a2 => arr.Except(new int[] { a1, a2 }).ToList().ForEach(a3 => builder.AppendLine(a1.ToString() + "," + a2.ToString() + "," + a3.ToString()))));
        Console.WriteLine(builder.ToString());
        File.WriteAllText("result.txt", builder.ToString());
        Console.ReadKey();
    }运行后到你的程序目录下看result.txt文件,里面是结果。
      

  7.   


    class Program
        {
            static int[] numlist = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            static void Main(string[] args)
            {
                int num = 0;
                string nunstr = "";
                for (int i = 0; i < numlist.Length-2; i++)
                {
                    for (int j = i + 1; j < numlist.Length - 1; j++)
                    {
                        for (int k = i + 2; k < numlist.Length; k++)
                        {
                            nunstr = numlist[i] + "," + numlist[j] + "," + numlist[k];
                            num++;
                            Console.WriteLine(nunstr);
                        }
                    }
                }
                Console.WriteLine(num.ToString());
            }
        }
      

  8.   

    想由哪些组成就修改这个List就可以了。
    List<int> arr = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
      

  9.   


    public static String[] GetAllCombination(Int32[] arr, Int32 n)  //取n个的组合 
            {
                Int32[] state = new Int32[arr.Length];
                List<String> list = new List<String>();            for (Int32 i = 0; i < n; i++)
                {
                    state[i] = 1;
                }
                for (Int32 i = n; i < state.Length; i++)
                {
                    state[i] = 0;
                }            String str = "";
                for(Int32 i = 0;i < n;i++)
                    str += arr[i] + ",";            list.Add(str.TrimEnd(','));            while (true)
                {
                    Boolean flag = true;
                    Int32 index = 0;
                    Int32 oneCount = 0;
                    Int32 zeroCount = 0;
                    StringBuilder sbuilder = new StringBuilder();                for (; index < state.Length - 1; index++)
                    {
                        if (state[index] == 1 && state[index + 1] == 0)
                        {
                            Int32 temp = state[index];
                            state[index] = state[index + 1];
                            state[index + 1] = temp;
                            break;
                        }
                    }                for (Int32 i = 0; i < index; i++)
                    {
                        if (state[i] == 0)
                            zeroCount++;
                        else
                            oneCount++;
                    }                for (Int32 i = 0; i < index; i++)
                    {
                        if (i < oneCount)
                            state[i] = 1;
                        else
                            state[i] = 0;
                    }                for (Int32 i = 0; i < arr.Length; i++)
                    {
                        if (state[i] == 1)
                            sbuilder.Append(arr[i] + ",");
                    }                list.Add(sbuilder.ToString().TrimEnd(','));                for (Int32 i = 0; i < arr.Length - n; i++)
                    {
                        if (state[i] == 1)
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                        break;
                }            return list.ToArray();
            }
            static void Main(string[] args)
            {
                Int32[] arr = { 1, 2, 3, 4, 5,6,7,8,9,10 };
                String[] list = GetAllCombination(arr, 3);
                foreach (String s in list)
                    Console.WriteLine(s);
            }
    修改的,取你的{1,2,3,4,5,6,7,8,9,10} 3个数的组合输出:
    1,2,3
    1,2,4
    1,3,4
    2,3,4
    1,2,5
    1,3,5
    2,3,5
    1,4,5
    2,4,5
    3,4,5
    1,2,6
    1,3,6
    2,3,6
    1,4,6
    2,4,6
    3,4,6
    1,5,6
    2,5,6
    3,5,6
    4,5,6
    1,2,7
    1,3,7
    2,3,7
    1,4,7
    2,4,7
    3,4,7
    1,5,7
    2,5,7
    3,5,7
    4,5,7
    1,6,7
    2,6,7
    3,6,7
    4,6,7
    5,6,7
    1,2,8
    1,3,8
    2,3,8
    1,4,8
    2,4,8
    3,4,8
    1,5,8
    2,5,8
    3,5,8
    4,5,8
    1,6,8
    2,6,8
    3,6,8
    4,6,8
    5,6,8
    1,7,8
    2,7,8
    3,7,8
    4,7,8
    5,7,8
    6,7,8
    1,2,9
    1,3,9
    2,3,9
    1,4,9
    2,4,9
    3,4,9
    1,5,9
    2,5,9
    3,5,9
    4,5,9
    1,6,9
    2,6,9
    3,6,9
    4,6,9
    5,6,9
    1,7,9
    2,7,9
    3,7,9
    4,7,9
    5,7,9
    6,7,9
    1,8,9
    2,8,9
    3,8,9
    4,8,9
    5,8,9
    6,8,9
    7,8,9
    1,2,10
    1,3,10
    2,3,10
    1,4,10
    2,4,10
    3,4,10
    1,5,10
    2,5,10
    3,5,10
    4,5,10
    1,6,10
    2,6,10
    3,6,10
    4,6,10
    5,6,10
    1,7,10
    2,7,10
    3,7,10
    4,7,10
    5,7,10
    6,7,10
    1,8,10
    2,8,10
    3,8,10
    4,8,10
    5,8,10
    6,8,10
    7,8,10
    1,9,10
    2,9,10
    3,9,10
    4,9,10
    5,9,10
    6,9,10
    7,9,10
    8,9,10