寒假作业

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了寒假作业相关的知识,希望对你有一定的参考价值。


11.一个游戏,前20关是每一关自身的分数,
            1-30关每一关是10分,31-40关,每一关是20分,
            1-49关,每一关是30分,第50关是100分,
            输入你现在闯到的关卡数,求你现在拥有的分数。
            利用if嵌套for。
            Console.Write("请输入你现在闯到的关卡数:");
            int a = int.Parse(Console.ReadLine());
            int sum = 0;
            if (a >= 1 && a <= 50)
            {
                for (int i = 1; i <= a; i++)
                {
                    if (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;
                    }
                }
            }
            else
            {
                Console.WriteLine("您输入的关卡数错误。");
            }
            Console.WriteLine(sum);
            Console.ReadLine();

 

12.利用if嵌套for。
            Console.Write("请输入你现在闯到的关卡数:");
            int a = int.Parse(Console.ReadLine());
            int sum=0;
            if (a > 0 && a <= 20)
            {
                  for(int i=0;i<=a;i++)
                  {
                      sum+=i;
                  }
            }
            else if(a>20&&a<=30)
            {
                for (int i = 0; i <= 20; i++)
                {
                    sum += i;
                }
                for (int i = 21; i <= a;i++ )
                {
                    sum += 10;
                }
            }
            else if (a > 30 && a <= 40)
            {
                for (int i = 0; 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 > 40 && a <= 49)
            {
                for (int i = 0; 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 if (a == 50)
            {
                for (int i = 0; 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();

 

13.输入月份和日期,输出是今年的第多少天。
            (2月按照28天计算)利用switch case。
            Console.Write("请输入月:");
            int m = int.Parse(Console.ReadLine());
            Console.Write("请输入日期:");
            int d = int.Parse(Console.ReadLine());
            int m1=31,m2=28,m3=31,m4=30,m5=31,m6=30,m7=31,m8=31,m9=30,m10=31,m11=30;
            switch(m)
            {
                case 1:
                    Console.WriteLine("第"+d+"天");
                    break;
                case 2:
                    Console.WriteLine("第"+(m1+d)+"天");
                    break;
                case 3:
                    Console.WriteLine("第" + (m1 + m2 + d) + "天");
                    break;
                case 4:
                    Console.WriteLine("第" + (m1 + m2 + m3 + d) + "天");
                    break;
                case 5:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4+d) + "天");
                    break;
                case 6:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + d) + "天");
                    break;
                case 7:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + d) + "天");
                    break;
                case 8:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + d) + "天");
                    break;
                case 9:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d) + "天");
                    break;
                case 10:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d) + "天");
                    break;
                case 11:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d) + "天");
                    break;
                case 12:
                    Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d) + "天");
                    break;
                default:
                    break;
            }
            Console.ReadLine();

 


14.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,
            总共只有100文钱,
            如何在凑够100只鸡的情况下刚好花完100文钱?利用for嵌套+if筛选。
            将每一种情况列出来,最终输出一个有**种买法

            如果只买公鸡,最多能买100/2=50     a
            只买母鸡,最多能买100                     b
            只买小鸡,最多能买200                      c
            要求刚好花完100      并凑够一百只鸡
            2*a + b +0.5*c ==100  &&  a+b+c==100
            int x = 0;
            for (int a = 0; a <= 50;a++ )
            {
                for (int b = 0; b <= 100; b++)
                {
                    for (int c = 0; c <= 100; c++)
                    {
                        if(a+b+c==100&&2*a+b+0.5*c==100)
                        {
                            x++;
                            Console.WriteLine("第"+x+"种方法:"+"公鸡"+a+"只,母鸡"+b+"只,小鸡"+c+"只。");
                        }
                    }
                }
            }
            Console.WriteLine("共方法"+x+"种");

 


15.大马驼2石粮食,中等马驼1石粮食,
            两头小马驼1石粮食,要用100匹马,
            驼100石粮食,该如何分配?利用for嵌套+if筛选。
            int x = 0;
            for (int a = 0; a <= 50;a++ )
            {
                for (int b = 0; b <= 100;b++ )
                {
                    for (int c = 0; c <= 200;c++ )
                    {
                        if(a+b+c==100&&2*a+1*b+0.5*c==100)
                        {
                            x++;
                            Console.WriteLine("大马"+a+"中马"+b+"小马"+c);
                        }
                    }
                }
            }
            Console.WriteLine("共有" + x + "结果");

以上是关于寒假作业的主要内容,如果未能解决你的问题,请参考以下文章

寒假作业随笔

LQ0029 寒假作业枚举+离线程序

《面向对象程序设计》——寒假作业3

寒假作业2

2017级面向对象程序设计寒假作业4

2018寒假作业2