穷举:

   穷举法的基本思想是根据题目的部分条件确定答案的大致范围, 并在此范围内对所有可能的情况逐一验证,直到全部情况验证完毕。若某个情况验证符合题目的全部条件,则为本问题的一个解;若全部情况验证后都不符合题目的全部条件, 则本题无解。穷举法也称为枚举法。

穷举练习:

int n = 1;
            for (int i = 0; 2 * i <= 100; i++)
            {
                for (int a = 0; 5 * a <= 100; a++)
                {
                    for (int k = 0; 10 * k <= 100; k++)
                    {
                        for (int x = 0; 15 * x <= 100; x++)
                        {
                            if (2 * i + 5 * a +10*k+15*x== 100)
                            {
                                Console.WriteLine(n);
                                Console.WriteLine("铅笔盒:{0}", a);
                                Console.WriteLine("铅笔:{0}", i);
                                Console.WriteLine("文件夹:{0}", k);
                                Console.WriteLine("彩笔:{0}", x);
                                n++;
                            }
                        }
                    }
                }
            }
            #endregion
            Console.ReadLine();

04-27 05:17