問題: https://codeiq.jp/ace/joboffer_apli/q677
クエリ式とメソッド形式を混ぜるのは、邪道なんだろうな。
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
var priceList = new[] { 0, 1, 6, 8, 10, 17, 18, 20, 24, 26, 30 };
var all = Partition(10, 10, List.Empty).Select(x => x.AsEnumerable());
var descending =
from l in all
orderby l.Select(x => priceList[x]).Sum() descending
select l;
Console.WriteLine(string.Join(",", descending.First()));
}
static IEnumerable<List> Partition(int number, int max, List list)
{
if (number == 0)
{
return Enumerable.Repeat<List>(list, 1);
}
return
from i in Enumerable.Range(1, Math.Min(max, number))
from c in Partition(number - i, i, list.Add(i))
select c;
}
}
class List
{
public readonly int? Head;
public readonly List Tail;
private List(int? head, List tail)
{
this.Head = head;
this.Tail = tail;
}
public static readonly List Empty = new List(null, null);
public List Add(int head)
{
return new List(head, this);
}
public IEnumerable<int> AsEnumerable()
{
for (var i = this; i != List.Empty; i = i.Tail)
{
yield return i.Head.Value;
}
}
}