换币种

        static void Main(string[] args)
        {
            // 只能用一次
            //IList<HUOWU> hUOWUs = new List<HUOWU>{
            //    new HUOWU(1,1),new HUOWU(2,5),new HUOWU(3,4),
            //    new HUOWU(6,7),new HUOWU(8,12),new HUOWU(9,15)
            //};
            //int bag = 10;
            //var rt = MaxValue(bag, hUOWUs);
            //Console.WriteLine($"最高:{rt}");

            // 纸张不可以无限使用
            int[] coins = { 5, 2, 3, 5 };
            int amount = 5;
            int n = coins.Length;
            int[,] dp = new int[n + 1, amount + 1];
            dp[n, 0] = 1;
            var rt = Change2(coins, amount, dp);
            //var rt = Change1(coins, amount);
            //
            Console.WriteLine(rt);
            Console.ReadKey();
        }

        static int Change2(IList<int> coins,int amount,  int[,] dp) {

            // 正下方 +  index + 1, rest - coins[index]
            for (int index = coins.Count - 1; index >= 0; index--)
            {
                for (int rest = 0; rest <= amount; rest++)
                {
                    dp[index, rest] = dp[index + 1, rest] + (rest - coins[index] >= 0 ? dp[index + 1, rest - coins[index]]:0);
                }
            }
            return dp[0, amount];
        }

        static int Change1(IList<int> coins, int amount)
        {
            if (amount <= 0) return 0;
            if (coins.Count() <= 0) return 0;

            return Process1(coins, 0, amount);
        }
        static int Process1(IList<int> coins, int index, int rest)
        {
            if (rest < 0) return 0;
            if (index == coins.Count()) return rest == 0 ? 1 : 0;

            return Process1(coins, index + 1, rest) + Process1(coins, index + 1, rest - coins[index]);
        }

 

posted @ 2023-04-24 14:03  vba是最好的语言  阅读(18)  评论(0编辑  收藏  举报