日期:2014-05-18 浏览次数:20431 次
public static void GetA2(int num) { //Num数字集合 Sum数字之和 List<string> list = new List<string>(); list = GetNum(list, 10, num, ""); list.Sort(); foreach (string str in list) { Console.WriteLine(str); } }
------解决方案--------------------
简单问题复杂化了
------解决方案--------------------
如果只有加的话,全排列吧
------解决方案--------------------
static void Main(string[] args) { int i = 6; int[] arr = Enumerable.Range(1, i).ToArray(); int sum = 10, c = 0; bool[] flag = new bool[i]; Calculate(arr, c, sum, flag); Console.ReadLine(); } public static void Calculate(int[] arr, int c, int sum, bool[] flag) { if (sum == 0) Output(arr, c, flag); else { if (c == arr.Length) return; else { flag[c] = true; if (sum - arr[c] >= 0) Calculate(arr, c + 1, sum - arr[c], flag); flag[c] = false; if (sum >= 0) Calculate(arr, c + 1, sum, flag); } } } public static void Output(int[] arr, int c, bool[] flag) { for (int i = 0; i < c; i++) { if (flag[i]) Console.Write("{0,2}", arr[i]); } Console.WriteLine(); } /* 1 2 3 4 1 3 6 1 4 5 2 3 5 4 6 */
------解决方案--------------------
2 8
3 7
算漏了
------解决方案--------------------
汗 不好意思 没有看到 i=6
------解决方案--------------------
呵呵,有意思~!
------解决方案--------------------
造个方法递归调用
------解决方案--------------------
public void Main() { num(10, 6); } /// <summary> /// /// </summary> /// <param name="value">匹配值</param> /// <param name="i">最大数</param> public void num(int value, int i) { num2(0, 0, value, i, ""); } public void num2(int i, int flag, int value, int maxvalue, string output) { int temp = flag + i; if (flag > 0) { if (output == "") { output = flag.ToString(); } else { output = output + "," + flag; } } if (temp == value) { Response.Write(output + "<br>"); Console.WriteLine(output); } else if (temp < value) { for (