帮忙求个算法,用C#写出用1分的硬币,2分的硬币,5分的硬币,拼成246分,有多种组合

解决方案 »

  1.   


     private static void sss()
            {
                
                for (int i = 0; i <= 246;i++ )
                {
                    for (int j = 0; j <= 246/2; j++)
                    {
                        for (int k = 0; k <= 246/5; k++)
                        {
                            if (i * 1 + j * 2 + k * 5 == 246)
                            {
                                Console.WriteLine(i+","+j+","+k);
                            }
                        }
                    }
                        
                }
            }
      

  2.   

    刚才发的是把组合显示全部显示出来,要想得到有多少种就看下面的 private static void ddd()
            {
                int d = 0;
                for (int i = 0; i <= 246; i++)
                {
                    for (int j = 0; j <= 246 / 2; j++)
                    {
                        for (int k = 0; k <= 246 / 5; k++)
                        {
                            if (i * 1 + j * 2 + k * 5 == 246)
                            {
                                d++;
                                
                            }
                        }
                    }            }
                Console.WriteLine(d);
            }
      

  3.   

    246=245+1=49*5+15=5
    5=1+1+1+1+1
    5=1+2+1+1
    5=1+2+2so 一共 49*4=196 种题目不难,直接降维计算就可以了。5以下是可穷举数,无须计算 直接swich给出穷举即可  伪代码:  int getCount(int i)
    {
      switch(i)
        case 0:
         retrun 0;
        case 1:
         return 0;
        case 2:
         retrun 2;
        case 3:
         return 2;
        case 4:
         return 3;
       
    }  n=256
     result=n/5*getCount(5)+getcount(n%5)
      

  4.   

    哦,确实是算法问题,用动态规划可以求,不过目前只有3种币值,用楼上的循环就可以。
    这是以前写的一个100元换零钱的
    using System;namespace CSharpTest
    {
        class Program
        {
            static int[] Items;
            static long[,] Matrix;        static void Main(string[] args)
            {
                Items = new int[] { 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5 ,2 ,1};
                int value = 10000;
                Matrix = new long[value + 1, Items.Length];
                Console.WriteLine(GetCount(0, value));
                Console.ReadKey();
            }        static long GetCount(int currentIndex, int remain)
            {
                //如果金额小于0,返回0种
                if (remain < 0)
                    return 0;            //如果金额=0,或算到1元了则正好可以被兑换
                if (remain == 0 || currentIndex == Items.Length - 1)
                    return 1;            if (Matrix[remain, currentIndex] == 0)
                    Matrix[remain, currentIndex] = GetCount(currentIndex + 1, remain) + GetCount(currentIndex, remain - Items[currentIndex]);            return Matrix[remain, currentIndex];
            }
        }
    }