#region 获取数组中(多个数)相加和等于(<=)给定值的算法
int[] myarray = { 1, 2,3,4,5,6,7,8,9,10,11 }; List<List<int>> mylist = new List<List<int>>(); int length = myarray.Length; for (int i = 0; i < Math.Pow(2, length); i++) { List<int> myint = new List<int>(); for (int j = 0; j < length; j++) { if (Convert.ToBoolean(i & (1 << j))) myint.Add(myarray[j]); } mylist.Add(myint); } foreach (var a in mylist) { if (a.Sum() == 20) { foreach (var b in a) { Console.Write(b); Console.Write(","); } Console.WriteLine(); }
}
#endregion
获取数组中(两个个数)相加和等于(<=)给定值的算法
#region 获取数组中(两个数)相加和等于(<=)给定值的算法 int[] myarray1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; for (int i = 0; i < myarray1.Length; i++) { for (int j = 0; j < myarray1.Length; j++) { if (myarray1[i] + myarray1[j] == 15) { Console.WriteLine(myarray1[i] +"和"+myarray1[j]); } } } #endregion