穷举 迭代 while

for循环拥有两类:

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

例:1.单位给发了一张150元购物卡,拿着到超市买三类洗化用品。洗发水15元,香皂2元,牙刷5元。求刚好花完150元,有多少种买法,没种买法都是各买几样?

         //洗发水 x  10
         //牙刷    y  30
         //香皂    z  75
            int ci = 0;
            int biao = 0;
            for (int x = 0; x <= 10; x++)
            {
                for (int y = 0; y <= 30; y++)
                {
                    for (int z = 0; z <= 75; z++)
                    {
                        ci++;
                        if (15 * x + y * 5 + z * 2 == 150)
                        {
                            biao++;
                            Console.WriteLine("第{0}种买法:洗发水{1}瓶,牙刷{2}支,香皂{3}块。", biao, x, y, z);
                        }
                    }
                }
            }
            Console.WriteLine("总共有{0}种买法。", biao);
            Console.WriteLine(ci);
            Console.ReadLine();

      2.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100文钱?同百马百担

            int n = 0;
            for (int g = 0; g * 2 <= 100; g++)
            {
                for (int m = 0; m <= 100; m++)
                {
                    for (int x = 0; x * 0.5 <= 100; x++)
                    {
                        if (g * 2 + m + x * 0.5 == 100 && g + m + x == 100)
                        {
                            Console.WriteLine("第{0}种:{1}只公鸡,{2}只母鸡,{3}只小鸡", n, g, m, x);
                            n++;
                        }
                    }
                }
            }
            Console.WriteLine("总共有{0}种可能性", n);
            Console.ReadLine();

     3.有1分钱,2分钱,5分钱的硬币,要组合出来2角钱,有几种组合方式,分别各多少个?

    int n = 0;
            for (int a = 0; a * 1 <= 20; a++)
            {
                for (int b = 0; b*2 <= 20; b++)
                {
                    for (int c = 0; c *5 <= 20; c++)
                    {
                        if (a * 1 + b*2 + c * 5 == 20)
                        {  
                            n++;
                            Console.WriteLine("第{0}种:{1}枚1分钱,{2}枚2分钱,{3}枚5分钱", n, a, b, c);
                         }
                    }
                }
            }
            Console.WriteLine("总共有{0}种可能性", n);
            Console.ReadLine(); 

迭代:初始情况按照规律不断求解中间情况,最终推导出结果。

例:1.五个小朋友排成一队,问第一个多大了,第一个说比第二个大两岁,问第二个多大了,第二个说比第三个大两岁。。。以此类推,问第5个小朋友,说自己3岁了。

        问第一个小朋友几岁了?

            int n = 1;
            int a = 3;
            while (n < 5)
            {
                a += 2;
                n++;
            }
            Console.WriteLine("第一个小朋友{0}岁了", a);
            Console.ReadLine();

     2.纸张可以无限次对折,纸张厚度为0.07毫米。问多少次对折至少可以超过8848?

            double height = 0.07;//8848m=8848000
            int ci = 0;
            while (height <= 8848000)
            {
                ci++;
                height *= 2;//height=height*2;
            }
            Console.WriteLine(ci);
            Console.ReadLine();

while 循环

其实是for循环的变形写法
for(int i = 1; i<=5;i++)
{
 循环体;
}

上面的for循环可以写成
int i= 1;
for(;i<=5;)
{
 循环体;
 i++;
}

写成while就是以下样式
int i= 1;
while(表达式(i<=5))
{
 循环体;
 状态改变(i++);
}

 

do
{
 循环体;
 状态改变(i++);
}while(表达式(i<=5))
注意:do while是不管满不满足表达式,我都会先执行一遍

 

跳转语句:
break:跳出整个循环
continue:跳出本次循环继续下次循环

转载于:https://www.cnblogs.com/ShenG1/p/5612677.html

你可能感兴趣的:(穷举 迭代 while)