日期:2014-05-17 浏览次数:21071 次
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { double[] myarray = { 1.1, 2.5, 3.3, 4.9, 5.8, 6.8, 7.5, 8.7, 9.4, 10.7, 11.2, 12.9, 13.6, 14.8, 15.9, 16.4, 17.7, 18.4, 19.2 }; int cnt = 0; foreach (var result in 寻找组合(myarray, 50.8, myarray.Length - 1)) { Console.Write("结果{0}==> ", ++cnt); foreach (var n in result) Console.Write("{0} ", n); Console.WriteLine(); } Console.WriteLine("--------------End"); Console.ReadKey(); } static IEnumerable<IEnumerable<double>> 寻找组合(double[] array, double sum, int index) { if (index >= 0) { foreach (var sub in 寻找组合(array, sum, index - 1)) yield return sub; foreach (var sub in 寻找组合(array, sum - array[index], index - 1)) yield return sub.Concat(new double[] { array[index] }); if (Math.Abs(array[index] - sum) <= double.Epsilon) yield return new double[] { array[index] }; } } } }
------解决方案--------------------