码迷,mamicode.com
首页 > 其他好文 > 详细

5月4日课堂内容:for循环的穷举、迭代

时间:2016-05-05 00:14:01      阅读:486      评论:0      收藏:0      [点我收藏+]

标签:

一、for循环拥有两类:

1穷举:

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

2迭代:

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

二、穷举练习

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

            int count = 0;//count数数的意思,多少种买法

            int bian = 0;//看循环总共走了多少遍

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

            {

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

                {

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

                    {

                        bian++;

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

                        {

                            count++;

                            Console.WriteLine("第" + count + "种买法:洗发水" + x + "瓶,牙刷" + y + "支,香皂" + z + "块!");

                        }

                    }

                }

            }

            Console.WriteLine("总共" + count + "买法!(符合题意的算法)");

            Console.WriteLine("总共循环" + bian + "遍!(总共有这么多算法,但符合题意得的是count种算法)");

            Console.ReadLine();

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

            int count = 0;

            int bian = 0;

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

            {

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

                {

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

                    {

                        bian++;

                        if (dm + zm + xm == 100 && dm * 2 + zm * 1 + xm * 0.5 == 100)

                        {

                            count++;

                            Console.WriteLine("第" + count + "种分配法:大马" + dm + "匹,中马" + zm + "匹,小马" + xm + "匹!");

                        }

                    }

                }

            }

            Console.WriteLine("总共" + count + "种分配方法!");

            Console.WriteLine("总共循环" + bian + "遍!");

            Console.ReadLine();

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

            int count = 0;

            int bian = 0;

            for (int yf = 0; yf <= 20; yf++)

            {

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

                {

                    for (int wf = 0; wf <= 4; wf++)

                    {

                        bian++;

                        if (yf * 1 + ef * 2 + wf * 5 == 20)

                        {

                            count++;

                            Console.WriteLine("第" + count + "种组合方法:一分" + yf + "个,二分" + ef + "个,五分" + wf + "个!");

                        }

                    }

                }

            }

            Console.WriteLine("总共" + count + "种组合方法!");

            Console.WriteLine("总共循环" + bian + "遍!");

            Console.ReadLine();

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

            int count = 0;

            int bian = 0;

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

            {

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

                {

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

                    {

                        bian++;

                        if (gong + mu + xiao == 100 && gong * 2 + mu * 1 + xiao * 0.5 == 100)

                        {

                            count++;

                            Console.WriteLine("第" + count + "种买法:公鸡" + gong + "只,母鸡" + mu + "只,小鸡" + xiao + "只!");

                        }

                    }

                }

            }

            Console.WriteLine("总共" + count + "种买法!(符合题意的算法)");

            Console.WriteLine("总共循环" + bian + "遍!(总共有这么多算法,但符合题意得的是count种算法)");

            Console.ReadLine();

三、迭代练习

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

第一种:

            int age = 3;

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

            {

                age += 2;

            }

            Console.WriteLine(age);

            Console.ReadLine();

第二种:

            int age = 3;

            for (int i = 5; i > 1; i--)

            {

                age += 2;

            }

            Console.WriteLine(age);

            Console.ReadLine();

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

第一种:

            double count = 0;

            for (double i = 0.07; i <= 8848000; i *= 2)

            {

                count++;

            }

            console.writeline(count);

            console.readline();

第二种:

            int count = 0;

            for (int i = 7; i <= 884800000; i *= 2)

            {

                count++;

            }

            Console.WriteLine(count);

            Console.ReadLine();

四、while格式的写法,及练习

(一)写法:

while格式就是for变形得到的,初始条件拿到前面,循环的状态改变放到循环体的最后一句,for变成while,将原先的分号去掉,只留下循环条件

例:

            int sum = 0;

            int i = 1;

            while (i <= 10)

            {

                sum += i;

                i++;

            }

            Console.WriteLine(sum);

(二)练习:

1纸张厚度0.07毫米,折叠多少次至少超过8848米

            int count = 0;

            double i = 0.07;

            while (i <= 8848000)

            {

                count++;

                i *= 2;//在本题中使用高度来限制次数,不确定要折叠多少次,所以i++此状态改变要去掉,记录次数count即可

            }

            Console.WriteLine(count);

            Console.ReadLine();

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

            int count = 0;

            int gong = 0;

            while (gong <= 50)

            {

                int mu = 0;

                while (mu <= 100)

                {

                    int xiao = 0;

                    while (xiao <= 200)

                    {

                        if (gong + mu + xiao == 100 && gong * 2 + mu * 1 + xiao * 0.5 == 100)

                        {

                            count++;

 

                            Console.WriteLine("第" + count + "种买法:公鸡" + gong + "只,母鸡" + mu + "只,小鸡" + xiao + "只!");

                        }

                        xiao++;

                    }

                    mu++;

                }

                gong++;

            }

            Console.WriteLine("总共" + count + "种买法!");

            Console.ReadLine();

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

            int age = 3;

            int i = 5;

            while ( i > 1)

            {

                age += 2;

                i--;

            }

            Console.WriteLine(age);

            Console.ReadLine();

4输入一个100以内的数,输入正确,累加求和,输入错误,打印“请重新输入!”

            for (; ; )

            {

                Console.Write("请输入一个100以内的正整数:");

                int a = int.Parse(Console.ReadLine());

                int sum = 0;

                if (a >= 0 && a <= 100)

                {

                    for (int j = 0; j <= a; j++)

                    {

                        sum += j;

                    }

                    Console.WriteLine(sum);

                    break;

                }

                else

                {

                    Console.WriteLine("您的输入有误,请重新输入!");

                }

            }

            Console.ReadLine();

5月4日课堂内容:for循环的穷举、迭代

标签:

原文地址:http://www.cnblogs.com/juyangchao12/p/5460099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!