• 穷举迭代03/10


                                                                                                       

    穷举迭代03/10

    for循环拥有两类:

    1、穷举:

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

    for循环拥有两类:

    2、穷举:

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

    for循环  for(int i = 1; i<=5;i++)

    {

      循环体;

    }

    while样式      int i= 1;

    while(表达式(i<=5))

    {

                      循环体;

                      状态改变(i++);

    }

    do while 样式    do

    {

                       循环体;

                       状态改变(i++);

    }while(表达式(i<=5))

    练习:

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

    //设洗发水x             150/15==10

                //牙刷y                   150/5==30

            //香皂z                   150/2==75

                int a = 0;

                int sum = 0;

                for (int x = 0; x <= 10; x++)

                {

                    for (int y = 0; y <= 30; y++)

                    {

                        for (int z = 0; z <= 75; z++)

                        {

                            sum++;

                            if (15 * x + 5 * y + 2 * z == 150)

                            {

                                a++;

                                Console.WriteLine("第" + a + "种买法,洗发水" + x + "瓶,牙刷" + y + "把,香皂" + z + "块。");

                            }

     

                        }

                    }

                }

                Console.WriteLine("共有" + a + "种买法。");

                Console.WriteLine("总共计算" + sum + "次。");

     

     

     

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

                //公鸡x,50

                //母鸡y,100

                //小鸡z,200

                int sum = 0;

                int sum1 = 0;

                for (int x = 0; x <= 50;x++ )

                {

                    for (int y = 0; y <= 100;y++ )

                    {

                        for (int z = 0; z <= 200;z++ )

                        {

                            sum1++;

                            if((x+y+z==100)&&(2*x+1*y+0.5*z==100))

                            {

                                sum++;

                                Console.WriteLine("第"+sum+"种方法:公鸡"+x+"只,母鸡"+y+"只,小鸡"+z+"只。");

                            }

                        }

                    }

                }

                Console.WriteLine("总共计算了"+sum1+"次。");

     

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

            int a = 7;

                int b = 884800000;

                for (int i = 1; ; i++)

                {

                    a *= 2;

                    if (a >= b)

                    {

                        Console.WriteLine("这是第" + i + "次折叠。");

                        Console.WriteLine("高度为" + a);

                        break;

                    }

            }

     

  • 相关阅读:
    CSS – rem, em, px
    CSS – Variables
    go等待子协程完成再结束
    go原生mysql链接
    [恢]hdu 2511
    linux中竖线'|',双竖线‘||’,&和&&的意思
    SSH隧道:端口转发功能详解
    shell脚本执行返回的状态码
    第1章 ssh命令和SSH服务详解
    shell脚本中各种括号的区别以及用法
  • 原文地址:https://www.cnblogs.com/zst062102/p/5263942.html
Copyright © 2020-2023  润新知