首页 > 代码库 > 循环语句、穷举

循环语句、穷举

#region --猜拳(三局两胜)--
Random r = new Random();
int x = r.Next(3);
int sum = 0;
Console.WriteLine("与电脑猜拳游戏");

for (int i = 0; i < 3; i++)
{
  Console.WriteLine("电脑已出拳,请出拳(石头-0,布-1,剪刀-2)");
  int r1 = int.Parse(Console.ReadLine());
  Console.WriteLine("电脑出拳" + x);
  Console.WriteLine("您出拳" + r1);
  if (x == 0)
  {
    if (r1 == 0)
    {
      Console.WriteLine("平局");
    }
    if (r1 == 1)
    {
      Console.WriteLine("玩家赢");
      sum = sum + 1;
    }
    if (r1 == 2)
    {
      Console.WriteLine("玩家输");
    }
  }
  if (x == 1)
  {
    if (r1 == 0)
    {
      Console.WriteLine("玩家输");
    }
    if (r1 == 1)
    {
      Console.WriteLine("平局");
    }
    if (r1 == 2)
    {
      Console.WriteLine("玩家赢");
      sum = sum + 1;
    }
  }
  if (x == 2)
  {
    if (r1 == 0)
    {
      Console.WriteLine("玩家赢");
      sum = sum + 1;
    }
    if (r1 == 1)
    {
      Console.WriteLine("玩家输");
    }
    if (r1 == 2)
    {
      Console.WriteLine("平局");
    }
  }
}
if (sum >= 2)
{
  Console.WriteLine("玩家赢");
}
else
{
  Console.WriteLine("玩家输");
}


# endregion
#region --求和--
int he1=0;
for (int a = 1; a < 101; a++)
{
  he1 = he1 + a;
}
Console.WriteLine("1-100的和是:"+he1);

int he2 = 0;
for (int a = 1; a < 101; a = a + 2)
{
  he2 = he2 + a;
}
Console.WriteLine("100内的奇数和是:"+he2);

int he3 = 0;
for (int a = 2; a < 101; a = a + 2)
{
  he3 = he3 + a;
}
Console.WriteLine("100内的偶数和是:"+he3);

int he4 = 0;
for (int a = 1; a < 101; a++)
{
  if (a % 7 == 0 || a / 10 == 7 || a % 10 == 7)
  {
    he4 = he4 + a;
  }
}
Console.WriteLine("100以内与7有关的数的和是:"+he4);

int he5 = 0;
for (int a = 1; a < 101; a++)
{
  int xx = 0;
  for (int b = 1; b < a; b++)
  {
    if (a % b == 0)
    {
      xx = xx + 1;
    }
  }
  if (xx == 1)
  {
    he5 = he5 + a;
  }
}
Console.WriteLine("100以内质数的和是:" + he5);
#endregion


#region --菱形--
//右上
for (int m = 1; m < 10; m++)
{
  for (int n = 1; n <= m; n++)
  {
    Console.Write("☆");
  }
  Console.Write("\n");
}

//右下
for (int m = 10; m > 1; m--)
{
  for (int n = 1; n < m; n++)
  {
    Console.Write("☆");
  }
  Console.Write("\n");
}

//左下
for (int m = 1; m < 10; m++)
{
  for (int n = 1; n < m; n++)
  {
    Console.Write(" ");
  }
  for (int l = 10; l > m; l--)
  {
    Console.Write("☆");
  }
  Console.Write("\n");
}

//左上
for (int m = 1; m < 10; m++)
{
  for (int n = 9; n >m; n--)
  {
    Console.Write(" ");
  }
  for (int l = 1; l <= m;l++ )
  {
    Console.Write("☆");
  }
  Console.Write("\n");
}

//上半部分
for (int m = 1; m < 10; m++)
{
  for (int n = 9; n > m; n--)
  {
    Console.Write(" ");
  }
  for (int l = 1; l <= (2 * m - 1); l++)
  {
    Console.Write("☆");
  }
  Console.Write("\n");
}
//下半部分
for (int m = 1; m < 9; m++)
{
  for (int l = 1; l <= m; l++)
  {
    Console.Write(" ");
  }
  for (int n = 1; n <= (2 * (9 - m) - 1); n++)
  {
    Console.Write("☆");
  }
  Console.Write("\n");
}
//完整
#endregion

#region --打印图案--
string[,] shuzu;
shuzu = new string[9, 9]{
{"★","★","★","★"," ","★","★","★","★"},
{"★","★","★","★"," ","★","★","★","★"},
{"★","★","★"," ","★"," ","★","★","★"},
{"★","★"," ","★"," ","★"," ","★","★"},
{"★"," ","★","★"," ","★","★"," ","★"},
{"★","★","★","★"," ","★","★","★","★"},
{"★","★","★","★"," ","★","★","★","★"},
{"★","★","★"," ","★"," ","★","★","★"},
{"★","★","★"," ","★"," ","★","★","★"},
};
for (int i = 0; i < 9; i++)
{
  for(int j=0;j<9;j++)
  {
    Console.Write(shuzu[i,j]);
  }
  Console.Write("\n");
}
#endregion

#region--小球弹起高度--
Console.WriteLine("请输入次数:");
int cishu =int.Parse(Console.ReadLine());
double h = 10;
for (int i = 1; i <= cishu; i++)
{
  h = h * 0.75;
}
Console.WriteLine(h);
#endregion


#region --兔子--
Console.WriteLine("输入几个月:");
int shuru = int.Parse(Console.ReadLine());
int youtu=1;
int xiaotu = 0;
int chengtu = 0;
for (int i = 2; i <= shuru; i++)
{
  chengtu = chengtu + xiaotu;
  xiaotu = youtu;
  youtu = chengtu;
}
Console.WriteLine("{0}个月后有{1}对成兔,{2}队小兔,{3}对幼兔,总共有{4}对兔子。",shuru,chengtu,xiaotu,youtu,chengtu + xiaotu + youtu);
#endregion

#region --穷举--
int ss = 0;
for(int m=1;m<50;m++)
{
  for (int n = 1; n < 20;n++ )
  {
    for (int l = 1; l < 10; l++)
    {
      for (int i = 1; i < 7; i++)
      {
        if (2 * m + 5 * n + 10 * l + 15 * i == 100)
        {
          ss = ss + 1;
          Console.WriteLine("第{0}种,铅笔{1}支,铅笔盒{2}个,文件夹{3}个,彩笔{4}盒,正好花完100元。",ss, n, m, l, i);
        }
      }
    }
  }
}
#endregion

循环语句、穷举