标签:add csharp div ring name har vat ati sum
Q:将数字 sum 分为 k组,5分成3组,"0 0 5" ,"0 1 4" ,0 2 3 ...,2 1 2.
/// <summary> /// 将数字 sum 分为 k组,5分成3组,"0 0 5" ,"0 1 4" ,0 2 3 ...,2 1 2. /// </summary> /// <param name="sum"></param> /// <param name="k"></param> /// <returns>"0 0 5" ,"0 1 4" ,0 2 3 ...,2 1 2</returns> public static List<string> C(int sum, int k) { List<string> result = new List<string>(); C2(sum, k, "", result); return result; } private static void C2(int sum, int k, string str, List<string> result) { if (sum >= 0) { if (k == 1) { result.Add(str + " " + sum); } else { for (int i = 0; i <= sum; i++) { C2(sum - i, k - 1, str + " " + i, result); } } } }
标签:add csharp div ring name har vat ati sum
原文地址:https://www.cnblogs.com/icemaker/p/11294394.html