9号的内容:上午主要是练习两个图题型,下午继续,只是在最后的时候讲的是迭代和穷举。关于这些内容我先整理下文字叙述性的内容,然后再把题型在练习一下。。。

循环:反复执行某段代码。

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

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

{

循环体

}

break ——中断循环,跳出循环

continue——停止本次循环,进入下次循环

for循环拥有两类:

穷举:

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

迭代:

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

while 循环

其实是for循环的变形写法

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

{

循环体;

}

上面的for循环可以写成

int i= 1

for(;i<=5;)

{

循环体;

i++;

}

写成while就是以下样式

int i= 1

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

{

循环体;

状态改变(i++);

}

do

{

循环体;

状态改变(i++;

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

注意:do while是不管满不满足表达式,我都会先执行一遍。

跳转语句:

break:跳出整个循环

continue:跳出本次循环,继续下次循环。

    练习:输入五遍你好

三月九号的内容复习-风君雪科技博客

三月九号的内容复习-风君雪科技博客

输入一个正整数,求阶乘

 Console.Write(“请输入一个正整数”);
            int a = int.Parse(Console.ReadLine());
            int sum = 1;
            for (int i = 1; i <= a; i++)
            {
                sum *= i;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

三月九号的内容复习-风君雪科技博客

  //输入一个正整数,求和
            Console.Write(“a=”);
            int a = int.Parse(Console.ReadLine());
            int sum = 0;
            for (int i = 1; i <= a; i++)
            {
                sum += i;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

三月九号的内容复习-风君雪科技博客

 //求1!+2!+3!+4!+…..+n!
            Console.Write(“请输入n=”);
            int n = int.Parse(Console.ReadLine());
            int sum = 0;
            for (int i = 1; i <= n; i++)
            {
                int sum1 = 1;
                for (int j = 1; j <= i; j++)
                {
                    sum1 *= j;
                }
                sum += sum1;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

三月九号的内容复习-风君雪科技博客

//找出100以内和7有关的数

            int a = 0;
            for (int i = 1; i <= 100; i++)
            {
                if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
                {
                    a++;
                    Console.WriteLine(i);
                }
            }
            Console.WriteLine(“共” + a + “次”);
            Console.ReadLine();

三月九号的内容复习-风君雪科技博客

//一个游戏,前20关是每一关自身的分数,
            //21-30关每一关是10分
            //31-40关,每一关是20分
            //41-49关,每一关是30分
            //50关,是100分

这题有两种解题思路,一种是for嵌套if,一种是if嵌套for,经验证两种的结果一样

            Console.Write(“请输入你的关数a=”);
            int a = int.Parse(Console.ReadLine());
            int sum = 0;
            if (a >= 0 && a <= 50)
            {
                for (int i = 1; i <= a; i++)
                {
                    if (i >= 0 && i <= 20)
                    {
                        sum += i;
                    }
                    else if (i >= 21 && i <= 30)
                    {
                        sum += 10;
                    }
                    else if (i >= 31 && i <= 40)
                    {
                        sum += 20;
                    }
                    else if (i >= 41 && i <= 49)
                    {
                        sum += 30;
                    }
                    else
                    {
                        sum += 100;
                    }
                }
                Console.WriteLine(sum);
            }

            else
            {
                Console.WriteLine(“输入有误”);
            }
            Console.ReadLine();

第二种是:

            Console.Write(“输入关数a=”);
            int a = int.Parse(Console.ReadLine());
            int sum = 0;
            if (a >= 0 && a <= 50)
            {
                if (a >= 1 && a <= 20)
                {
                    for (int i = 1; i <= a; i++)
                    {
                        sum += i;
                    }
                }
                else if (a >= 21 && a <= 30)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= a; i++)
                    {
                        sum += 10;
                    }
                }

                else if (a >= 31 && a <= 40)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= a; i++)
                    {
                        sum += 20;
                    }
                }
                else if (a >= 41 && a <= 49)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= 40; i++)
                    {
                        sum += 20;
                    }
                    for (int i = 41; i <= a; i++)
                    {
                        sum += 30;
                    }
                }
                else
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= 40; i++)
                    {
                        sum += 20;
                    }
                    for (int i = 41; i <= 49; i++)
                    {
                        sum += 30;
                    }
                    sum += 100;
                }
            }
            else
            {
                Console.WriteLine(“输入有误”);
            }
            Console.WriteLine(sum);
            Console.ReadLine();

三月九号的内容复习-风君雪科技博客

 //乘法口诀

            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(“” + j + “*” + i + “=” + (i * j) + ” “);
                }
                Console.WriteLine();
            }
            Console.ReadLine();

三月九号的内容复习-风君雪科技博客

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

            int biao = 0;
            int m = 0;
            for (int x = 0; x <= 10; x++)
            {
                for (int y = 0; y <= 30; y++)
                {
                    for (int z = 0; z <= 75; z++)
                    {
                        m++;
                        if (15 * x + 5 * y + 2 * z == 150)
                        {
                            biao++;
                            Console.WriteLine(“这是第” + biao + “中共有洗发水” + x + “瓶,香皂” + z + “块,牙刷” + y + “只”);
                        }
                    }

                }
            }
            Console.WriteLine(“共有” + biao + “中”);
            Console.WriteLine(m);
            Console.ReadLine();

三月九号的内容复习-风君雪科技博客截取一部分,25916是运行的次数

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

            //0.07—-7,884800000
            int a = 7;
            int i = 0;   //int i=1;
            for (; ; )
            {
                a *= 2;
                i++;
                if (a >= 884800000)
                {
                    Console.WriteLine(a);
                    Console.WriteLine(i);
                    break;
                }
                         //i++;
            }
            Console.ReadLine();

三月九号的内容复习-风君雪科技博客几何倍增的威力是很大的

以上就是内容的复习