递归练习2

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"

posted on 2020-07-21 11:27  icemaker  阅读(139)  评论(0)    收藏  举报