首页 > 其他 > 详细

递归练习2

时间:2020-07-21 16:06:49      阅读:64      评论:0      收藏:0      [点我收藏+]

39分8组,取1-15之间的数值,不重复

C 39 8 = 3种

C 39 6 =139 种

C 39 4 = 47 种

   private static void C2(int sum, int k, int current, string str, List<string> result)
        {
            if (sum >= 0)
            {
                if (k == 1)
                {
                    if (sum >= 1 && sum <= 15 && current - 1 < sum && current <= 15)
                        result.Add(str + " " + sum);
                }
                else
                {
                    for (int i = current; i <= 15; i++)
                    {
                        C2(sum - i, k - 1, i + 1, str + " " + i, result);
                    }
                }
            }
        }

   
        public static List<string> C(int sum, int k, int current)
        {
            List<string> result = new List<string>();
            C2(sum, k, current, "", result);
            return result;
        }

" 1 2 3 4 5 6 7 11"

" 1 2 3 4 5 6 8 10"

" 1 2 3 4 5 7 8 9"

递归练习2

原文:https://www.cnblogs.com/icemaker/p/13354112.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!