#region 获取数组中(多个数)相加和等于(<=)给定值的算法 
     int[] myarray = { , ,,,,,,,,, };
List<List<int>> mylist = new List<List<int>>();
int length = myarray.Length;
for (int i = ; i < Math.Pow(, length); i++)
{
List<int> myint = new List<int>();
for (int j = ; j < length; j++)
{
if (Convert.ToBoolean(i & ( << j)))
myint.Add(myarray[j]);
}
mylist.Add(myint);
}
foreach (var a in mylist)
{
if (a.Sum() == )
{
foreach (var b in a)
{
Console.Write(b); Console.Write(",");
}
Console.WriteLine();
}
      }
    #endregion

C#中获取数组中相加和最接近或等于(&lt;=)给定值的算法-LMLPHP

获取数组中(两个个数)相加和等于(<=)给定值的算法

       #region 获取数组中(两个数)相加和等于(<=)给定值的算法
int[] myarray1 = { , , , , , , , , , , };
for (int i = ; i < myarray1.Length; i++)
{
for (int j = ; j < myarray1.Length; j++)
{
if (myarray1[i] + myarray1[j] == )
{
Console.WriteLine(myarray1[i] +"和"+myarray1[j]);
}
}
}    #endregion

C#中获取数组中相加和最接近或等于(&lt;=)给定值的算法-LMLPHP

05-11 22:04