リストがあるとします:
List<int> _arr = new List<int> {1, 3, 4};
そして、対象は4
指定されたリストからlinqを使用して{1, 3}
、1 + 3 = 4
そのまま返したいと思います。{4}
4 = 4
それ、どうやったら出来るの?
列挙子/リストのすべてのサブセットを取得するメソッドがあれば簡単です
(ここにあります: Answer: Most Elegant Way to Get All Subsets of an Array in C# )
using System;
using System.Collections.Generic;
using System.Linq;
public static class Program
{
static void Main(string[] args)
{
var test = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var target = 6;
var matches = from subset in test.SubSetsOf()
where subset.Sum() == target
select subset;
Console.WriteLine("Numbers: {0}", test.Select(i => i.ToString()).Aggregate((a, n) => a + ", " + n));
Console.WriteLine("Target: {0}", target);
foreach (var match in matches)
{
Console.WriteLine(match.Select(m => m.ToString()).Aggregate((a, n) => a + " + " + n) + " = " + target.ToString());
}
Console.ReadKey();
}
public static IEnumerable<IEnumerable<T>> SubSetsOf<T>(this IEnumerable<T> source)
{
// Deal with the case of an empty source (simply return an enumerable containing a single, empty enumerable)
if (!source.Any())
return Enumerable.Repeat(Enumerable.Empty<T>(), 1);
// Grab the first element off of the list
var element = source.Take(1);
// Recurse, to get all subsets of the source, ignoring the first item
var haveNots = SubSetsOf(source.Skip(1));
// Get all those subsets and add the element we removed to them
var haves = haveNots.Select(set => element.Concat(set));
// Finally combine the subsets that didn't include the first item, with those that did.
return haves.Concat(haveNots);
}
}
出力:
Numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9
Target: 6
1 + 2 + 3 = 6
1 + 5 = 6
2 + 4 = 6
6 = 6