循环语句 练习题

时间:2023-02-22 14:44:11

1.输入一个整数,计算从1加到这个数的结果

            Console.WriteLine("输入一个整数");
            int a = int.Parse(Console.ReadLine());
            int sum = 0;
            for (int i = 0; i <= a; i++)
            {
                sum += i;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

2. 找出100以内与7有关的数并打印
            (1).从1找到100
            (2).找出与7有关的数
                个位数是7 a%10==7
                十位数是7 a/10 ==7
                能被7整除 a%7 ==0

            for (int a = 1; a <= 100; a++)
            {
                if (a % 10 == 7 || a / 10 == 7 || a % 7 == 0)
                {
                    Console.Write(a + "\t");
                }
            }
            Console.ReadLine();

3. 一个游戏,前20关是每一关自身的分数
            21-30关每一关是10分
            31-40关,每一关是20分
            41-49关,每一关是30分
            50关,是100分
            输入你现在闯到的关卡数,求你现在拥有的分数

            Console.WriteLine("输入关数");
            int a = int.Parse(Console.ReadLine());
            int b=0;
            if (a > 0 && a <= 50)
            {
                for(int n=0;n<=a;n++)
                {
                    if(n<=20)
                    {
                        b+=n;
                    }
                    else if(n<=30)
                    {
                        b+=10;
                    }
                    else if(n<=40)
                    {
                        b+=20;
                    }
                    else if(n<=49)
                    {
                        b+=30;
                    }
                    else
                    {
                        b+=100;
                    }
                }
                Console.WriteLine(b);
            }
            else
            {
                Console.WriteLine("输入错误");
            }
            Console.ReadLine();

4.输入一个整数,求1!+2!+...+n!

            Console.WriteLine("输入一个整数");
            int a = int.Parse(Console.ReadLine());
            int sum = 0;
            for (int i = 1; i <= a; i++)
            {
                int jie = 1;
                for (int j = 1; j <= i; j++)
                {
                    jie *= j;
                }
                sum += jie;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

5.100以内的奇数的和

            int a = 0;
            for (int i = 0; i <= 100; i++)
            {
                if (i % 2 != 0)
                {
                    a += i;
                }
            }
            Console.WriteLine(a);
            Console.ReadLine();

6.99口诀表,一行一行的打印

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

7. 有一张超大的纸:
            纸张的厚度是0.07
            对折多少次,可以到达珠峰的高度,
            按照8848来计算  

            double a = 0.07;
            int i = 0;
            while (a <= 8848000)
            {
                i++;
                a *= 2;
            }
            Console.WriteLine(i);
            Console.ReadLine();  

8. 理想状态下,篮球*落体弹起高度若为上一次高度的5分之4
    首先高度设置为20米,输入一个次数,求篮球现在弹起的高度   

            Console.WriteLine("输入次数");
            int a=int.Parse(Console.ReadLine());
            double b = 20;
            for (int i = 1; i <= a; i++)
            {
                b = b * 4 / 5;
            }
            Console.WriteLine(b);
            Console.ReadLine();

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

            int a = 0;
            for (int x = 0; x <= 10; x++)
            {
                for (int y = 0; y <= 30; y++)
                {
                    for (int z = 0; z <= 75; z++)
                    {
                        if (15 * x + 5 * y + 2 * z == 150)
                        {
                            Console.WriteLine(x + "洗发水" + y + "牙刷" + z + "香皂");
                            a++;
                        }
                    }
                }
            }
            Console.WriteLine(a);
            Console.ReadLine();

10.百鸡百钱:公鸡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 + y + 0.5 * z == 100)
                        {
                            Console.WriteLine(x + "只公鸡" + y + "只母鸡" + z + "只小鸡");
                            a++;
                        }
                    }
                }
            }
            Console.WriteLine(a);
            Console.ReadLine();

11.  有1分钱,2分钱,5分钱的硬币,要组合出来1元钱,有几种组合方式,分别各多少个?
            int n = 0;
            for (int a = 0; a <= 100; a++)
            {
                for (int b = 0; b <= 50; b++)
                {
                    for (int c = 0; c <= 20; c++)
                    {
                        if (a + 2 * b + 5 * c == 100)
                        {
                            Console.WriteLine(a + "枚一分" + b + "枚两分" + c + "枚五分");
                            n++;
                        }
                    }
                }
            }
            Console.WriteLine("共" + n + "种");
            Console.ReadLine();

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

            int a = 3;
            int i = 1;
            while (i < 5)
            {
                a = a + 2;
                i++;
            }
            Console.WriteLine(a);
            Console.ReadLine();