比如,写个简单的C#例子,要求如下:
输入2个数字 第一个为范围,第2个为总数
要求 在1-范围内 找到所有相加等于总数的集合(集合必须为5)并且这5个数字在本组内不能重复
比如 范围=d 总数=c 
5个数分别是a1,a2,a3,a4,a5
必须满足 a1<d a2<d a3<d a4<d a5<d 并且 a1!=a2!=a3!=a4!=a5 并且 a1+a2+a3+a4+a5=c看谁的例子最简洁.希望大家集思广益多多发言.

解决方案 »

  1.   

    比如 范围=d 总数=c如2楼的例子:1+2+3+4+5=15 至少d必须大于等于5,c必须大于等于15。
      

  2.   

    第一步,先判定是否存在答案
    d>5&&c>15&&5d-10>c
    算法等下再说
      

  3.   

    int max = 10, total = 27;for (int a = 1; a <= max; a++)
    {
        for (int b = a + 1; b <= max; b++)
        {
            for (int c = b + 1; c <= max; c++)
            {
                for (int d = c + 1; d <= max; d++)
                {
                    for (int e = d + 1; e <= max; e++)
                    {
                        if (a + b + c + d + e == total)
                        {
                            Console.WriteLine("{0} + {1} + {2} + {3} + {4} = {5}", a, b, c, d, e, total);
                        }
                    }
                }
            }
        }
    }
      

  4.   

    int max = 10, total = 27;for (int a = 1; a < max; a++)
    {
        for (int b = a + 1; b < max; b++)
        {
            for (int c = b + 1; c < max; c++)
            {
                for (int d = c + 1; d < max; d++)
                {
                    for (int e = d + 1; e < max; e++)
                    {
                        if (a + b + c + d + e == total)
                        {
                            Console.WriteLine("{0} + {1} + {2} + {3} + {4} = {5}", a, b, c, d, e, total);
                        }
                    }
                }
            }
        }
    }
      

  5.   

    int max = 10, total = 27;for (int a = 1; a < max; a++)
    {
        for (int b = a + 1; b < max; b++)
        {
            for (int c = b + 1; c < max; c++)
            {
                for (int d = c + 1; d < max; d++)
                {
                    for (int e = d + 1; e < max; e++)
                    {
                        if (a + b + c + d + e == total)
                        {
                            Console.WriteLine("{0} + {1} + {2} + {3} + {4} = {5}", a, b, c, d, e, total);
                            break;//还是加一个好
                        }
                    }
                }
            }
        }
    }
      

  6.   

    int max = 20, count = 6, total = 27;int[] result = new int[count];
    for (int i = 0; i < count; i++)
    {
        result[i] = i + 1;
    }while (result[0] < max - count)
    {
        for (int i = count - 1; i >= 0; i--)
        {
            if (result[i] < max - count + i)
            {
                result[i]++;
                for (int j = i + 1; j < count; j++)
                {
                    result[j] = result[j - 1] + 1;
                }
                break;
            }
        }    int sum = 0;
        foreach (int i in result) sum += i;
        if (sum == total)
        {
            foreach (int i in result) Console.Write("{0} ", i);
            Console.Write("= {0}\r\n", total);
        }
    }