首页 > 其他分享 >换币种

换币种

时间:2023-04-24 14:11:47浏览次数:21  
标签:index int coins 币种 rest amount new

        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]);
        }

 

标签:index,int,coins,币种,rest,amount,new
From: https://www.cnblogs.com/Insist-Y/p/17349325.html

相关文章

  • PayPal对接不同币种的坑
    问题场景:做跨境电商网站,支持多币种支付,对接PayPal支付时,美元、欧元、英镑等都能付款成功,但是日元却无法付款成功。对接PayPal的时候不同币种的要求不一样,PayPal对于日元不......