穷举法和迭代法

穷举法:把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况

洗发水15元一瓶  牙刷5元一个  肥皂2元    共150元

 1             int a = 0;
 2             for (int x = 0; x <= 10; x++)
 3             {
 4                 for (int y = 0; y <= 30; y++)
 5                 {
 6                     for (int z = 0; z <= 75; z++)
 7                     {
 8                         if (x * 15 + y * 5 + z * 2 == 150)
 9                         {
10                             a++;
11                             Console.WriteLine("" + a + "种方法:洗发水" + x + "瓶  牙刷" + y + "个  肥皂" + z + "");
12                         }
13                     }
14                 }
15             }

百鸡百钱  公鸡5分钱一只  母鸡3文钱一只  雏鸡一分钱两只   100文钱买100只鸡

 1             int a = 0;
 2             for (int g = 0; g <= 20; g++)
 3             {
 4                 for (int m = 0; m <= 33; m++)
 5                 {
 6                     for (int c = 0; c <= 100; c++)
 7                     {
 8                         if (g * 5 * 2 + m * 3 * 2 + c == 200 && g + m + c == 100)
 9                         {
10                             a++;
11                             Console.Write("" + a + "种方法:");
12                             Console.WriteLine("公鸡" + g + "只 母鸡" + m + "只  雏鸡" + c + "");
13                         }
14                     }
15                 }
16             }

百马百担   大马驼2石粮食  中马驼一石粮食  两匹小马驼一石粮食  100匹马驼100石粮食

 1             int a = 0;
 2             for (int d = 0; d <= 50; d++)
 3             {
 4                 for (int z = 0; z <= 100; z++)
 5                 {
 6                     for (int x = 0; x <= 100; x += 2)
 7                     {
 8                         if (d * 2 + z + x / 2 == 100 && d + z + x == 100)
 9                         {
10                             a++;
11                             Console.WriteLine("大马" + d + "匹  中马 " + z + "匹  小马" + x + "");
12                         }
13                     }
14                 }
15             }
16             Console.WriteLine("" + a + "种方法");

一分钱  二分钱  五分钱组合成两角的硬币  有多少种组合方法

 1             int a = 0;
 2             for (int x = 0; x <= 20; x++)
 3             {
 4                 for (int y = 0; y <= 10; y++)
 5                 {
 6                     for (int z = 0; z <= 4; z++)
 7                     {
 8                         if (x + y * 2 + z * 5 == 20)
 9                         {
10                             a++;
11                             Console.WriteLine("一分钱" + x + "个  二分钱" + y + "个  五分钱" + z + "");
12                         }
13                     }
14                 }
15             }
16             Console.WriteLine("" + a + "种方法");

迭代法

一张纸厚0.07毫米  问对折多少次能超过8848米 

            int h = 7;
            int i = 1;
            for (; ; i++)
            {
                h *= 2;
                if (h > 884800000)
                {
                    Console.WriteLine("需要" + i + "次对折");
                    break;
                }
            }

幼兔一个月后长成小兔,小兔一个月后长成成兔,成兔一个月后生下幼兔,问几个月后幼兔小兔成兔各多少

 1             Console.Write("请输入月数:");
 2             int y = int.Parse(Console.ReadLine());
 3             int a = 0; //成兔
 4             int b = 0; //小兔
 5             int c = 1; //幼兔
 6             int d = 1;// 总兔对数
 7             for (int i = 1; i <= y; i++)
 8             {
 9                 if (i == 1)
10                 {
11                     a = 0; //成兔
12                     b = 0; //小兔
13                     c = 1; //幼兔
14 
15                 }
16                 else
17                 {
18                     a = b + a;  //本月成兔=上月小兔+上月成兔
19                     b = c;      //本月小兔=上月幼兔
20                     c = a;      //本月幼兔=上月成兔
21                 }
22                 d = a + b + c;
23             }
24             Console.WriteLine("本月的成兔为" + a + "本月的小兔为" + b + "本月的幼兔为" + c + "本月的总兔为" + d);
原文地址:https://www.cnblogs.com/zk0533/p/5263993.html