1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CIQ48のフィギュアを10体売る

Last updated at Posted at 2014-01-15

問題: 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;
        }
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?