using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace SumFunc ...{ publicpartialclass Form1 : Form ...{ /**////<summary> /// The List used to store the Testing Data ///</summary> private List<int> intArr; /**////<summary> /// The List used to store the ready result member ///</summary> private List<int> pendingArr; /**////<summary> /// The List used the store the Final Result ///</summary> private List<string> lines; /**////<summary> /// The Int Array used to initialize the Testing Data ///</summary> privateint[] Arr; /**////<summary> /// The Const Sum Result ///</summary> privateconstint sumResult =59; public Form1() ...{ InitializeComponent(); Arr =newint[] ...{ 4, 56, 23, 59, 2, 26, 1, 16, 52, 3 }; intArr =new List<int>(); pendingArr =new List<int>(); lines =new List<string>(); intArr.AddRange(Arr); } privatevoid Btn_Sum_Click(object sender, EventArgs e) ...{ if (intArr.Count >0) ...{ for (int i =0; i < intArr.Count; i++) ...{ //Every run should clear the pendingArr so that there's no unwanted ready result member pendingArr.Clear(); //Select one record int tmpInt = intArr[i]; if (tmpInt == sumResult) ...{ listResult(tmpInt.ToString()); continue; } if (tmpInt > sumResult) continue; List<int> tmpArr =new List<int>(); tmpArr.AddRange(Arr); //Exclude the selected record tmpArr.RemoveAt(i); int tmpRst = tmpInt; for (int j =0; j < tmpArr.Count; j++) ...{ tmpRst += tmpArr[j]; if (tmpRst < sumResult) ...{ //Get a good ready result member pendingArr.Add(tmpArr[j]); continue; } if (tmpRst > sumResult) ...{ //bad member, cut it tmpRst -= tmpArr[j]; continue; } if (tmpRst == sumResult) ...{ //so, we get the right sum formular //do forget the add the right member into the pendingArr pendingArr.Add(tmpArr[j]); string aRst = tmpInt.ToString() +"+"; for (int k =0; k < pendingArr.Count; k++) ...{ aRst += pendingArr[k].ToString(); if (k != pendingArr.Count -1) ...{ aRst +="+"; } } listResult(aRst); } } } } } /**////<summary> /// Handle the Result ///</summary> ///<param name="result">The Result Formular</param> privatevoid listResult(string result) ...{ result = sortResult(result); if (!lines.Contains(result)) ...{ //A RichTextBox to show the Result richTextBox1.Text += result +""; lines.Add(result); } } /**////<summary> /// Sort the result in Ascending ///</summary> ///<param name="result">The Result Formular</param> ///<returns>The Result in Ascending</returns> privatestring sortResult(string result) ...{ string rst =string.Empty; List<int> list =new List<int>(); string[] pieces = result.Split('+'); if (pieces.Length >0) ...{ foreach (string piece in pieces) ...{ list.Add(int.Parse(piece)); } } if (list.Count >0) ...{ //Using the List<T> sort the result in Ascending list.Sort(); //Then, output the result in formular mode for (int i =0; i < list.Count; i++) ...{ rst += list[i].ToString(); if (i != list.Count -1) ...{ rst +=" + "; } else ...{ rst +=" = "+ sumResult.ToString(); } } } return rst; } } }