1. 程式人生 > >用1元,2元,5元,10元,20元和50元的紙幣組成100元,共有多少種情況

用1元,2元,5元,10元,20元和50元的紙幣組成100元,共有多少種情況

  static void Main(string[] args)
        {
            int count = 0;
            //1元組成的情況,最多有100種
            for (int a = 0; a <= 100; a++)
            {
                //2元的情況,最多有50種可能
                for (int b = 0; b <= 50; b++)
                {
                    for (int c = 0; c <= 20; c++)
                    {
                        //10元的情況,最多10種可能
                        for (int d = 0; d <= 10; d++)
                        {
                            //20元的情況,最多5種可能
                            for (int e = 0; e <= 5; e++)
                            {
                                //50元的情況,最多2種可能
                                for (int f = 0; f <= 2; f++)
                                {
                                    if ((1 * a + 2 * b + 5 * c + 10 * d + 20 * e + 50 * f)== 100)
                                    {
                                        count++;
                                        Console.WriteLine("1*{0}+2*{1}+5*{2}+10*{3}+20*{4}+50*{5}=100",
                                        a, b, c, d, e, f);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine("共有{0}種可能情況", count);    //count=4562
            Console.ReadKey();
        }