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

for语句应用举例

时间:2014-08-19 16:23:14      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:blog   http   for   2014   log   line   c++   ad   

语句

  顺序语句

  分支语句

  循环语句

    1、初始条件

    2、循环条件

    3、循环体

    4、状态改变

  for(初始条件;循环条件;状态改变)

  {

    循环体;

  }

 

    穷举法

    迭代法

 

 1、

 bubuko.com,布布扣

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

            int n = Convert.ToInt32(Console.ReadLine());

 

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

            {

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

                {

                    Console.Write("  ");

                }

                for (int b = 1; b <= (2 * i - 1); b++)

                {

                    Console.Write("☆");

                }

                Console.Write("\n");

            }

 

            for (int j = 1; j < n ; j++)

            {

                for (int c = 1; c <= j; c++)

                {

                    Console.Write("  ");

                }

                for (int d = 1; d <= 2*(n - j) - 1; d++)

                {

                    Console.Write("☆");

                }

                Console.Write("\n");

            }

 

2、一篮球从10米高落下,每次弹起高度是上次高度的3/4,输入次数,求弹起高度

 

            Console.Write("请输入次数:");

            int n = Convert.ToInt32(Console.ReadLine());

            double h = 10;

 

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

            {

                h*=3/4;

                Console.WriteLine("弹起高度" + h);

            }

 

 

3、一对幼兔一个月后长成小兔(每对兔子默认一公一母),再过一个月长成成兔并且生下一对小兔,以此类推,两年后有多少对兔子?

 

            int a = 1, b = 0, c = 0, m = 0, n = 0;

 

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

            {

                if (i == 1)

                {

                    m = 1;

                    n = 0;

                    c = 0;

                }

                else

                {

                    m = b + c;

                    n = a;

                    c = m;

                    a = m;

                    b = n;

                }

 

                Console.WriteLine(i);

                Console.WriteLine("幼兔:" + m);

                Console.WriteLine("小兔:" + n);

                Console.WriteLine("大兔:" + c);

                Console.WriteLine("总数:" + (m + n + c));

            }

 

for语句应用举例,布布扣,bubuko.com

for语句应用举例

标签:blog   http   for   2014   log   line   c++   ad   

原文地址:http://www.cnblogs.com/phantom-k/p/3922252.html

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