zoukankan      html  css  js  c++  java
  • 从数列1,2,3.......n 中 随意取几个数,使其和等于 m

      //从数列1,2,3.......n 中 随意取几个数,使其和等于 m
      
            public static void Print(int n, int m, List<int> list = null)
            {
                if (n < 1 || m < 1)
                {
                    return;
                }

                for (int i = 1; i <= n; i++)
                {
                    if (i < m)
                    {
                        List<int> childList = new List<int>();

                        if (list != null)
                        {
                            childList.AddRange(list);
                        }
                        childList.Add(i);

                        Print(i - 1, m - i, childList);
                    }
                    else if (i == m)
                    {
                        if (list != null)
                        {
                            foreach (int j in list)
                            {
                                Console.Write("{0} + ", j);
                            }

                            Console.WriteLine("{0}", m);
                        }
                    }
                }
            }

  • 相关阅读:
    面试题目小结
    面试题目3
    C#中new和override区别
    [转]:存储过程与函数的区别
    [转载]:C# 面试题大全
    [转]:C++虚函数表解析
    【修订版】C#/ASP.Net 面试题及答案(1)
    [转载]:C# 中结构与类的区别
    [转载]:C#笔试题面试题锦集
    [转载]:SQL Server性能调优之执行计划深度剖析 第一节 浅析SQL执行的过程
  • 原文地址:https://www.cnblogs.com/dasydong/p/3280970.html
Copyright © 2011-2022 走看看