11.一个游戏,前20关是每一关自身的分数,1-30关每一关是10分,31-40关,每一关是20分,1-49关,每一关是30分,第50关是100分,输入你现在闯到的关卡数,求你现在拥有的分数。利用if嵌套for。
Console.Write("输入你闯的关卡数:");
int a = int.Parse(Console.ReadLine());
sum = 0;
if(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 j = 21; j <= a;j++ )
{
sum += 10;
}
}
else if(a>=31&&a<=40)
{
for (int i = 1; i <= 20; i++)
{
sum += i;
}
for (int j = 21; j <= 30; j++)
{
sum += 10;
}
for (int k = 31; k <=a;k++ )
{
sum += 20;
}
}
else if (a >= 41 && a <= 49)
{
for (int i = 1; i <= 20; i++)
{
sum += i;
}
for (int j = 21; j <= 30; j++)
{
sum += 10;
}
for (int k = 31; k <= 40; k++)
{
sum += 20;
}
for (int l = 41; l <= a;l++ )
{
sum += 30;
}
}
else//a==50
{
for (int i = 1; i <= 20; i++)
{
sum += i;
}
for (int j = 21; j <= 30; j++)
{
sum += 10;
}
for (int k = 31; k <= 40; k++)
{
sum += 20;
}
for (int l = 41; l <= 49; l++)
{
sum += 30;
}
sum = sum + 100;
}
Console.WriteLine("总分为:" + sum);
12.一个游戏,前20关是每一关自身的分数,1-30关每一关是10分,31-40关,每一关是20分,1-49关,每一关是30分,第50关是100分,输入你现在闯到的关卡数,求你现在拥有的分数。利用for嵌套if。
Console.Write("输入你闯的关卡数:");
int a = int.Parse(Console.ReadLine());
sum = 0;
for (int i = 1; i <= a;i++ )
{
if(i>=1&&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);
13.输入月份和日期,输出是今年的第多少天。(2月按照28天计算)利用switch case。
int sum = 0;
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:
sum = d;
break;
case 2:
sum = m1 + d;
break;
case 3:
sum = m1 + m2 + d;
break;
case 4:
sum = m1 + m2 +m3+ d;
break;
case 5:
sum = m1 + m2 + m3 +m4+ d;
break;
case 6:
sum = m1 + m2 + m3 + m4+m5 + d;
break;
case 7:
sum = m1 + m2 + m3 + m4 + m5+m6 + d;
break;
case 8:
sum = m1 + m2 + m3 + m4 + m5 + m6+m7 + d;
break;
case 9:
sum = m1 + m2 + m3 + m4 + m5 + m6 + m7+m8 + d;
break;
case 10:
sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8+m9 + d;
break;
case 11:
sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9+m10 + d;
break;
case 12:
sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10+m11 + d;
break;
}
Console.WriteLine("今年的第" +sum+ "天");
14.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100文钱?利用for嵌套+if筛选。
int sum = 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)
{
sum++;
Console.WriteLine("第"+sum+"种方法:"+x+"只公鸡,"+y+"只母鸡,"+z+"只小鸡。");
}
}
}
}
Console.WriteLine("共有" + sum + "中方法。");
15.大马驼2石粮食,中等马驼1石粮食,两头小马驼1石粮食,要用100匹马,驼100石粮食,该如何分配?利用for嵌套+if筛选。
int sum = 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)
{
sum++;
Console.WriteLine("第"+sum+"种方法:"+x+"匹大马,"+y+"匹中马,"+z+"匹小马。");
}
}
}
}
Console.WriteLine("共有"+sum+"中方法。");
16.纸张可以无限次对折,纸张厚度为0.07毫米。问多少次对折至少可以超过8848?利用while。
int a = 7, b = 884800000;
int i=1;
while(true)
{
a*=2;
if(a>=b)
{
Console.WriteLine("第"+i+"次折叠。");
Console.WriteLine("高度为"+a);
break;
}
i++;
}
17.兔子生兔子。除了第一个月之后的所有月份都可以直接执行。成兔=上个月的成兔加上上个月的小兔。小兔=上个月的幼兔。幼兔=上个月的成兔加上上个月的小兔(即这个月的成兔)利用for嵌套if。
Console.Write("请输入几个月:");
int a = int.Parse(Console.ReadLine());
int cheng = 0, xiao = 0, you = 0,zong=0;
for (int i=1; i <= a;i++ )
{
if(i==1)
{
cheng = 0;
xiao = 0;
you = 1;
zong = 0;
}
else
{
cheng = cheng + xiao;
xiao = you;
you = cheng;
zong = cheng + xiao + you;
}
Console.WriteLine("第" + i + "月总兔为" + zong + "对,成兔" + cheng + "对,小兔" + xiao + "对,幼兔" + you + "对。");
}
18.输入两个时间日期,计算出相差多少天(TotalDays)。
Console.Write("请输入第一个时间:");
DateTime time1 = DateTime.Parse(Console.ReadLine());
Console.Write("请输入第二个时间:");
DateTime time2 = DateTime.Parse(Console.ReadLine());
Console.WriteLine("两个日期相差" + (time2 - time1).TotalDays + "天");
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 >= sum2 && sum4 >= sum3 && sum4 >= sum1 && sum4 >= sum5)
{
Console.WriteLine("胜出的是4号选手。");
}
else
{
Console.WriteLine("胜出的是5号选手。");
}
20.输入手机号码个数,根据手机号码个数创建数组。做抽奖活动。利用System.Threading.Thread.Sleep(3000);//程序暂停3秒
Console.Write("请输入人数:");
int a = int.Parse(Console.ReadLine());
string[] sj = new string[a];
for (int i = 0; i < a; i++)
{
Console.Write("输入第" + (i + 1) + "个人的手机号码:");
sj[i] = Console.ReadLine();
}
Console.WriteLine("输入完毕,请按回车键开始抽奖!");
Console.ReadLine();
Random ran = new Random();
int r = 0;
for (; ; )
{
int aa = ran.Next(a);
Console.Clear();
Console.WriteLine(sj[aa]);
System.Threading.Thread.Sleep(50);
if (r == 59)
{
//Console.Clear();//作弊代码
//Console.WriteLine("110");
break;
}
r++;
}