两个条件:
1:输入一个数 X 和: 20<=X<=165;
2:一个数N:个数  1<=N<=5;
例如
X=100; N=5;
因为N=5 所以就有  X1+X2+X3+X4+X5=100;
X1到X5 都是小于等于35大于等于1的,且彼此不重复
再例如:X=120;N=3
那么就是  X1+X2+X3=120;X1到X3 都是小于等于35 大于等于1的,且彼此不重复
依次类推!
要求把所有的可能都列举出来!不能遗漏

解决方案 »

  1.   

    X1+X2+X3=120,是举例吧!这个值就算是35+35+35也不等于120呀!
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace XN {
        class Program {
            private static int m_level = 0;
            private static int m_x = 20;
            private static int m_n = 1;
            private static int m_s = 1;        private static int[] m_numbers = new int[6];        static void Main(string[] args) {
                Console.Write("Input X(20 <= X <= 165): ");
                m_x = int.Parse(Console.ReadLine());
                Console.Write("Input N(1 <= X <= 5): ");
                m_n = int.Parse(Console.ReadLine());            Console.WriteLine("Running...");
                TryNextNumber(0);
                Console.WriteLine("OK.");            Console.ReadLine();
            }        static void TryNextNumber(int st) {
                ++m_level;            for (int i = st + 1; i <= 35; ++i) {
                    m_numbers[m_level - 1] = i;                if (m_level == m_n) {
                        // 目标迭代层数
                        ValidateNumbers();
                    } else {
                        TryNextNumber(i);
                    }
                }            --m_level;
            }        static void ValidateNumbers() {
                int sum = 0;
                for (int i = 0; i < m_level; ++i) {
                    sum += m_numbers[i];
                }            if (sum == m_x) {
                    Console.Write("{0, 2:D}.  ", m_s++);
                    for (int i = 0; i < m_level; ++i) {
                        Console.Write("{0,2:D}  ", m_numbers[i]);
                    }
                    Console.Write("\r\n");
                }
            }
        }
    }
      

  3.   

    http://topic.csdn.net/u/20101012/13/a6381631-dd9d-4d0e-87b9-b3fb1ef6fc2e.html
    思路差不多..
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.Text;namespace addtest
    {
        class Program
        {
            static int N, X;        static void Main(string[] args)
            {
                bool Nflag = true, Xflag = true;
                while (Nflag)
                {
                    Console.WriteLine("请输入参数N(N大于等于1小于等于5):");
                    string strN = Console.ReadLine();
                    if (strN == "exit")
                    {
                        return;
                    }
                    try
                    {
                        N = Convert.ToInt16(strN);
                        Nflag = false;
                    }
                    catch
                    {
                        N = 0;
                    }
                }
                while (Xflag)
                {
                    Console.WriteLine("请输入参数X(X大于等于20小于等于165):");
                    string strX = Console.ReadLine();
                    if (strX == "exit")
                    {
                        return;
                    }
                    try
                    {
                        X = Convert.ToInt16(strX);
                        Xflag = false;
                    }
                    catch
                    {
                        X = 0;
                    }
                }            
                for (int i = 1; i < 36; i++)
                {
                    addx(0, i, 1, "");
                }            Console.ReadLine();
            }        public static void addx(int AddX, int thisX, int thisN, string writeStr)
            {
                string thiswriteStr;
                int thisAddX;
                if (thisN > N)
                {
                    //thiswriteStr = writeStr + "N溢出!"
                    //Console.WriteLine(thiswriteStr);
                    return;
                }
                
                thiswriteStr = writeStr + "(X" + thisN.ToString() + ":" + thisX.ToString() + ")";
                thisAddX = AddX + thisX;
                
                if (thisAddX > (X - (N - thisN)))
                {
                    //thiswriteStr = writeStr + "X溢出!"
                    //Console.WriteLine(thiswriteStr);
                    return;
                }            if ((thisN == N) && (thisAddX==X))
                {
                        Console.WriteLine(thiswriteStr);
                }
                for (int i = 1; i < 36; i++)
                {
                    addx(thisAddX, i, thisN + 1, thiswriteStr);
                }
                return;
            }
        }
    }
      

  5.   

    凑合能算,数再大的话,需要把剪枝部分(还有最后1个数的计算)优化一下,不考虑输出的话,速度提高个10倍是有可能的
    using System;namespace CsdnTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Solve(5, 100, 35, string.Empty);
                Console.ReadKey();
            }        static void Solve(int remain, int remainSum, int max, string selected)
            {
                if (remain * max < remainSum || max < 0 || remainSum < 0)
                    return;            if (remain == 0 && remainSum == 0)
                {
                    Console.WriteLine(selected);
                    return;
                }            Solve(remain, remainSum, max - 1, selected);
                Solve(remain - 1, remainSum - max, max - 1, selected + max.ToString() + " ");
            }
        }
    }
      

  6.   

    优化下using System;
    using System.Collections.Generic;
    using System.Text;namespace addtest
    {
        class Program
        {
            static int N, X;        static void Main(string[] args)
            {
                bool Nflag = true, Xflag = true;
                while (Nflag)
                {
                    Console.WriteLine("请输入参数N(N大于等于1小于等于5):");
                    string strN = Console.ReadLine();
                    if (strN == "exit")
                    {
                        return;
                    }
                    try
                    {
                        N = Convert.ToInt16(strN);
                        Nflag = false;
                    }
                    catch
                    {
                        N = 0;
                    }
                }
                while (Xflag)
                {
                    Console.WriteLine("请输入参数X(X大于等于20小于等于165):");
                    string strX = Console.ReadLine();
                    if (strX == "exit")
                    {
                        return;
                    }
                    try
                    {
                        X = Convert.ToInt16(strX);
                        Xflag = false;
                    }
                    catch
                    {
                        X = 0;
                    }
                }            
                for (int i = 1; i < 36; i++)
                {
                    if (addx(0, i, 1, ""))
                    {
                        break;
                    }
                }            Console.ReadLine();
            }        public static bool addx(int AddX, int thisX, int thisN, string writeStr)
            {
                string thiswriteStr;
                int thisAddX;
                if (thisN > N)
                {
                    //thiswriteStr = writeStr + "N溢出!"
                    //Console.WriteLine(thiswriteStr);
                    return true;
                }
                
                thiswriteStr = writeStr + "(X" + thisN.ToString() + ":" + thisX.ToString() + ")";
                thisAddX = AddX + thisX;
                
                if (thisAddX > (X - (N - thisN)))
                {
                    //thiswriteStr = writeStr + "X溢出!"
                    //Console.WriteLine(thiswriteStr);
                    return true;
                }            if ((thisN == N) && (thisAddX==X))
                {
                        Console.WriteLine(thiswriteStr);
                }
                for (int i = 1; i < 36; i++)
                {
                    if (addx(thisAddX, i, thisN + 1, thiswriteStr))
                    {
                        break;
                    }
                }
                return false;
            }
        }
    }
      

  7.   

    简单优化了一下,可以算稍微大一点的,比如10个不超过40的数,和为200,解的数量本身就是指数级的,不会有太好的方法。如果只求解的数量,可以用动态规划。using System;namespace CsdnTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                int selectCount = 10, sum = 200, max = 40;
                Solve(selectCount, sum, max, new int[selectCount]);
                Console.ReadKey();
            }        static void Solve(int remainCount, int remain, int max, int[] selected)
            {
                if (max < remainCount || remain < remainCount || remainCount * max < remain)
                    return;            if (remainCount == 1 && remain <= max)
                {
                    selected[remainCount - 1] = remain;
                    //需要输出就去掉注释,为测试剪枝幅度,注释掉了
                    //for(int i = selected.Length - 1; i >= 0; i--)
                    //    Console.Write("{0} ", selected[i]);                //Console.WriteLine();
                    return;
                }            Solve(remainCount, remain, max - 1, selected);
                selected[remainCount - 1] = max;
                Solve(remainCount - 1, remain - max, max - 1, selected);
            }
        }
    }
      

  8.   

    把输出部分的注释去掉
    using System;namespace CsdnTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                int selectCount = 5, sum = 100, max = 35;
                Solve(selectCount, sum, max, new int[selectCount]);
                Console.ReadKey();
            }        static void Solve(int remainCount, int remain, int max, int[] selected)
            {
                if (max < remainCount || remain < remainCount || remainCount * max < remain)
                    return;            if (remainCount == 1 && remain <= max)
                {
                    selected[remainCount - 1] = remain;
                    //需要输出就去掉注释,为测试剪枝幅度,注释掉了
                    for(int i = selected.Length - 1; i >= 0; i--)
                        Console.Write("{0} ", selected[i]);                Console.WriteLine();
                    return;
                }            Solve(remainCount, remain, max - 1, selected);
                selected[remainCount - 1] = max;
                Solve(remainCount - 1, remain - max, max - 1, selected);
            }
        }
    }