典型90問    次の典型90問へ    前の典型90問へ

典型90問 016 Minimum Coins(★3)


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("227");
            WillReturn.Add("21 47 56");
            //5
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("9999");
            WillReturn.Add("1 5 10");
            //1004
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("998244353");
            WillReturn.Add("314159 265358 97932");
            //3333
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("100000000");
            WillReturn.Add("10001 10002 10003");
            //9998
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        long[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        long N = wkArr[0];

        SplitAct(InputList[1]);
        long A = wkArr[0];
        long B = wkArr[1];
        long C = wkArr[2];

        var AnswerKouho = new List<long>();
        for (long I = 0; I <= 9999; I++) {
            for (long J = 0; J <= 9999; J++) {
                long RestVal = N - A * I - B * J;
                if (RestVal < 0) break;

                if (RestVal % C > 0) continue;

                long Syou = RestVal / C;

                AnswerKouho.Add(I + J + Syou);
            }
        }
        Console.WriteLine(AnswerKouho.Min());
    }
}


解説

9999枚以下という制約を使い
2重ループで解いてます。