• 两类for循环


    九、两类for循环

    (一)穷举

    1、格式

     for (初始条件;循环条件 ;循环改变)
     {
         for (初始条件;循环条件;循环改变)
         {
             for (初始条件;循环条件;循环改变)
             {
                 if (判断条件)
                 {
                     Console.WriteLine(……);
                 }
             }
         }
     }

    其本质就是for虚幻嵌套。

    2、例题

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

    主要代码:

     int c = 0;
     int s = 0;
     for (int x = 0; x <= 10; x++)
     {
         for (int y = 0; y <= 30; y++)
         {
             for (int z = 0; z <= 75; z++)
             {
                 s++;
                 if (15 * x + 5 * y + 2 * z == 150)
                 {
                     c++;
                     Console.WriteLine("第{0}中买法能买{1}瓶洗发水,{2}支牙刷,{3}块香皂。", c, x, y, z);
                 }
             }
         }
     }
     Console.WriteLine("共有{0}种买法,只有{1}种买法符合要求。", s, c);
     Console.ReadLine();

    结果:

    2.2.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下(每种鸡至少有1只)刚好花完100文钱?

    主要代码:

    int c1 = 0;
    int s1 = 0;
    for (int g = 1; g < 50; g++)
    {
        for (int m = 1; m < 100; m++)
        {
            for (int x = 1; x < 200; x++)
            {
                s1++;
                if (g + m + x == 100 && 2 * g + 1 * m + 0.5 * x == 100)
                {
                    c1++;
                    Console.WriteLine("第{0}中买法能买{1}只公鸡,{2}只母鸡,{3}小鸡。", c1, g, m, x);
                }
            }
        }
    }
    Console.WriteLine("共有{0}种买法,只有{1}种买法符合要求。", s1, c1);
    Console.ReadLine();

    结果:

    2.3.大马驼2石粮食,中等马驼1石粮食,两头小马驼1石粮食,要用100匹马,驼100石粮食,该如何分配?

    主要代码:

    int c = 0;
    int s = 0;
    for (int d = 0; d <= 100; d++)
    {
        for (int z = 0; z <= 100; z++)
        {
            for (int x = 0; x <= 200; x++)
            {
                s++;
                if (2 * d + 1 * z + 0.5 * x == 100 && d + z + x == 100)
                {
                    c++;
                    Console.WriteLine("第{0}中驼法能用{1}匹大马,{2}匹中马,{3}匹小马。", c, d, z, x);
                }
            }
        }
    }
    Console.WriteLine("共有{0}种驼法,只有{1}种驼法符合要求。", s, c);
    Console.ReadLine();

    结果:

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

    主要代码:

     int c = 0;
     int s = 0;
     for (int i = 0; i <= 20; i++)
     {
         for (int j = 0; j <= 10; j++)
         {
             for (int k = 0; k <= 4; k++)
             {
                 s++;
                 if (i + 2 * j + 5 * k == 20)
                 {
                     c++;
                     Console.WriteLine("第{0}中组合方式需要{1}个1分钱,{2}个2分钱,{3}个5分钱。", c, i, j, k);
                 }
             }
         }
     }
     Console.WriteLine("共有{0}种组合方式,只有{1}种方式符合要求。", s, c);
     Console.ReadLine();

    结果:

    (二)迭代

    1、定义

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

    2、例题

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

    主要代码:

     int a =3;
     for (int b = 5; b > 1; b--)
     {
         a += 2;
     }
     Console.WriteLine("第一个小孩的年龄为{0}岁。",a);
     Console.ReadLine();

    结果:

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

    主要代码:

    int c = 0;
    double h = 0.07;
    while (h <= 8848000)
    {
        h *= 2;
        c++;
    }
    Console.WriteLine("需要对折{0}次。",c);
    Console.ReadLine();

    结果:

    (三)附加题

    求输入一个100以内的数的累加和,输入错了继续输入直到输入正确为止。

    主要代码:

     while (true)
     {
         Console.Write("请输入:");
         int n = int.Parse(Console.ReadLine());
         if (n >= 0 && n <= 100)
         {
             int s = 0;
             for (int i = 0; i <= n; i++)
             {
                 s += i;
             }
             Console.WriteLine(s);
         }
     }
     Console.ReadLine();

    结果:

  • 相关阅读:
    HDU 2089 不要62
    HDU 5038 Grade(分级)
    FZU 2105 Digits Count(位数计算)
    FZU 2218 Simple String Problem(简单字符串问题)
    FZU 2221 RunningMan(跑男)
    FZU 2216 The Longest Straight(最长直道)
    FZU 2212 Super Mobile Charger(超级充电宝)
    FZU 2219 StarCraft(星际争霸)
    FZU 2213 Common Tangents(公切线)
    FZU 2215 Simple Polynomial Problem(简单多项式问题)
  • 原文地址:https://www.cnblogs.com/bosamvs/p/5459492.html
Copyright © 2020-2023  润新知