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 > 0 && a <= 50) { if (a > 0 && a <= 20) { for (int i = 0; i < a; i++) { sum += i; } } else if (a >= 21 && a <= 30) { for (int i = 0; i <= 20; i++) { sum += i; } for (int i = 21; i <= a; i++) { sum += 10; } } else if (a >= 31 && 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 >= 41 && 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; } Console.WriteLine(sum); } else { Console.WriteLine("输入有误"); } Console.ReadLine();
12.一个游戏,前20关是每一关自身的分数,1-30关每一关是10分,31-40关,每一关是20分,1-49关,每一关是30分,第50关是100分,输入你现在闯到的关卡数,求你现在拥有的分数。利用for嵌套if。
Console.Write("请输入你的关数"); int a = int.Parse(Console.ReadLine()); int sum = 0; if (a > 0 && a <= 50) { for (int i = 0; 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();
13.输入月份和日期,输出是今年的第多少天。(2月按照28天计算)利用switch case。
int m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30; Console.Write("请输入月份"); int m = int.Parse(Console.ReadLine()); Console.Write("请输入日期"); int d = int.Parse(Console.ReadLine()); 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; } Console.ReadLine();
14.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100文钱?利用for嵌套+if筛选。
int biao = 0; for (int g = 0; 2 * g <= 100; g++) { for (int m = 0; m <= 100; m++) { for (int x = 0; 0.5 * x <= 100; x++) { if (2 * g + m + 0.5 * x == 100 && g + m + x == 100) { biao++; Console.WriteLine("第" + biao + "种共有公鸡" + g + "只,母鸡" + m + "只,小鸡" + x + "只"); } } } } Console.WriteLine("一共有" + biao + "种"); Console.ReadLine();
15.大马驼2石粮食,中等马驼1石粮食,两头小马驼1石粮食,要用100匹马,驼100石粮食,该如何分配?利用for嵌套+if筛选。
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 + "匹"); } } } } Console.ReadLine();
16.纸张可以无限次对折,纸张厚度为0.07毫米。问多少次对折至少可以超过8848?利用while。
int i = 0; int a = 7; while (a <= 884800000) { a *= 2; i++; } Console.WriteLine(a); Console.WriteLine(i); Console.ReadLine();
17.兔子生兔子。除了第一个月之后的所有月份都可以直接执行。成兔=上个月的成兔加上上个月的小兔。小兔=上个月的幼兔。幼兔=上个月的成兔加上上个月的小兔(即这个月的成兔)利用for嵌套if。
Console.Write("请输入月份"); int a = int.Parse(Console.ReadLine()); int y = 1; int x = 0; int c = 0; int zong = 0; for (int i = 0; i <= a; i++) { if (i == 1) { y = 1; x = 0; c = 0; zong = 1; } else { c = x + c; x = y; y = c; zong = x + y + c; } } Console.WriteLine("第" + a + "个月共" + zong + "对,有幼兔" + y + "对,小兔" + x + "对,成兔" + c + "对"); Console.ReadLine();
19.五个候选班长,20人投票。输入1-5来表示支持哪(个人)最后查看票数,看那个人胜出。不是1~5之内的数视为票作废。利用数组。
string[]shuzu=new string[20]; int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0; for (int i = 0; i < 20; i++) { Console.Write("第"+(i+1)+"个人支持的号码是:"); shuzu[i] = Console.ReadLine(); if (shuzu[i] =="1") { sum1++; } else if (shuzu[i] == "2") { sum2++; } else if (shuzu[i] == "3") { sum3++; } else if (shuzu[i] == "4") { sum4++; } else if (shuzu[i] == "5") { sum5++; } else { Console.WriteLine("输入有误作废"); } } Console.WriteLine("所有人都已选择完毕"); Console.WriteLine("1号选手得票数:"+sum1); Console.WriteLine("2号选手得票数:" + sum2); Console.WriteLine("3号选手得票数:" + sum3); Console.WriteLine("4号选手得票数:" + sum4); Console.WriteLine("5号选手得票数:" + sum5); if (sum1 > sum2 && sum1 > sum3 && sum1 > sum4 && sum1 > sum5) { Console.WriteLine("1号选手获胜"); } else if (sum2> sum1 && sum2 > sum3 && sum2 > sum4 && sum2 > sum5) { Console.WriteLine("2号选手获胜"); } else if (sum3 > sum2 && sum3 > sum1 && sum3 > sum4 && sum3 > sum5) { Console.WriteLine("3号选手获胜"); } else if (sum4 > sum1 && sum4 > sum3 && sum4 > sum2 && sum4 > sum5) { Console.WriteLine("4号选手获胜"); } else { Console.WriteLine("5号选手获胜"); } Console.ReadLine();
20.输入手机号码个数,根据手机号码个数创建数组。做抽奖活动。利用System.Threading.Thread.Sleep(3000);//程序暂停3秒
Console.Write("请输入电话号码的个数"); int n = int.Parse(Console.ReadLine()); string[] shuzu = new string[n];//把这n个手机号放到数组中 for (int i = 0; i < n; i++) { Console.Write("第" + (i + 1) + "个人的手机号是"); shuzu[i] = Console.ReadLine();//接受数组 } Console.WriteLine("所有号码输入完毕,按回车"); Console.ReadLine(); Random ran = new Random();//初始化 int m = 0; for (; ; ) { int nn = ran.Next(n);//随机从n中抽取 Console.Clear();//清屏 Console.WriteLine(shuzu[nn]); System.Threading.Thread.Sleep(100);//100毫秒 if (m == 29) //0.1*30=3秒 { break; } m++; } Console.ReadLine();
时间: 2024-12-28 14:31:11