zoukankan      html  css  js  c++  java
  • while和for的内嵌

     迭代,从初始情况按照规律不断求解中间情况,最终推导出结果。(折纸珠峰)

    穷举:把所有情况都列举一遍,选择符合条件的选项(百鸡百钱)

    循环四要素:初始条件,循环条件,循环体,状态改变。

    While的使用:

     Console.Write("请输入一个数字");

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

                int sum = 0;

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

                {

                    sum += i;

                }

                Console.WriteLine("数字的和是:"+sum);

                Console.ReadLine();

      Console.Write("请输入一个数字");

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

                int sum = 0;

                int i = 1;

               while ( i <= a)

                {

                   sum += i;

                   i++ ;

                }

                Console.WriteLine("数字的和是:"+sum);

                Console.ReadLine();

    随堂联系:

      //百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,

                //小鸡半文钱一只,总共只有100文钱,

                //如何在凑够100只鸡的情况下刚好花完100文钱?

                //int a = 0;

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

                //{

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

                //    {

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

                //        {

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

                //            {                         

                //                Console.WriteLine("公鸡{0}个,母鸡{1}个,小鸡{2}个,正好一百只鸡也是一百文钱",x,y,z);

                //                   a++;

                //            }

                //        }

                //    }

                //}

                //Console.WriteLine("总共{0}次",a);

                //Console.ReadLine();

                

                 //纸张可以无限次对折,纸张厚度为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();

                //现有1分,2分,5分钱无数

                //求组成1元钱,有多少种组合方式

                //int a = 0;

                //for (int x = 0; x <= 20;x++)

                //{

                //    for (int y = 0; y <= 50;y++)

                //    {

                //        for (int z = 0; z <= 100;z++)

                //        {

                //            if (5*x+2*y+z==100)

                //            {

                //                a++;

                //                Console.WriteLine("五分{0}个,二分{1}个,一分{2}个",x,y,z);

                //            }

                //        }

                //    }                     

                //}

                //Console.WriteLine("一共有组合方式{0}种",a);

                //Console.ReadLine();

                

      //五个小朋友排成一队,问第一个多大了,

                //第一个说比第二个大两岁,问第二个多大了,

                //第二个说比第三个大两岁。。。以此类推,

                //问第5个小朋友,说自己3岁了。问第一个小朋友几岁了?

                //int sum = 3;

                //for (int i = 0; i < 4;i++ )

                //{

                //    sum = sum + 2;

                //}

                //Console.WriteLine("年龄是"+sum);

                //Console.ReadLine();

  • 相关阅读:
    JavaScript 对联广告、漂浮广告封装类(IE,FF,Opera,Safari,Chrome)
    一个程序员的梦想
    无刷新分页控件(原创)(jQuery+json+ashx)(Ajax)
    Ajax无刷新分页(jQuery+Json)
    飞信 .net接口
    STL学习小记起因
    C++山寨CSharp事件
    在win8上花了一上午的闲暇做的贪吃蛇sample, 顺便移植到了WPF...
    STL学习小记顺序容器
    最近做的一个store app音乐箱
  • 原文地址:https://www.cnblogs.com/light3857/p/5611358.html
Copyright © 2011-2022 走看看