zoukankan      html  css  js  c++  java
  • 递归 换零钱问题——由打靶子问题引申

    原文发布时间为:2009-03-01 —— 来源于本人的百度文章 [由搬家工具导入]

    using System;
    //using System.Collections.Generic;
    //using System.Text;
    /*有任意张1毛,2毛,5毛钱,有几种组合可以组成9毛钱的方法。用递归实现并输出各种组合方法。*/
    namespace digui1
    {
        public class Class3
        {
            public static int sum = 0;
            public static int[] a = new int[] { 1, 2, 5 };
            public static int[] store = new int[9];
            public static void Main()
            {
                fun(9, 0);
                Console.WriteLine("总数为{0}", sum);
                Console.ReadLine();
            }

            public static void fun(int money, int k)
            {
                if (money< 0)
                    return;
                if (money == 0)
                {
                    Output(k);
                    return;
                }
                for (int i = 0; i < 3; i++)
                {
                    if (k == 0 || (k > 0 && a[i]>= store[k - 1]))//递增输出防止重复
                    {
                        store[k] = a[i];
                        fun(money - a[i], k + 1);
                    }
                }
            }
            public static void Output(int k)
            {
                for (int i = 0; i < k; i++)
                {
                    Console.Write("{0} ", store[i]);
                }
                Console.WriteLine();
                sum++;
            }
        }
    }

  • 相关阅读:
    mdx 根据维度Hierarchy节点的名字来filter节点,搜索节点
    学习C++.Primer.Plus 8 函数探幽
    学习C++.Primer.Plus 7 函数
    学习C++.Primer.Plus 6 分支语句和逻辑操作符
    学习C++.Primer.Plus 5 循环和关系表达式
    学习C++.Primer.Plus 4 复合类型
    NYoj_171聪明的kk
    NYoj_104最大和
    希尔排序
    NYoj_49开心的小明
  • 原文地址:https://www.cnblogs.com/handboy/p/7148506.html
Copyright © 2011-2022 走看看