zoukankan      html  css  js  c++  java
  • C# 课堂总结5-数组

    一、

    数组:解决同一类大量数据在内存存储和运算的功能。

    1、一维数组
    定义:制定类型,指定长度,指定名称。
    int[] a=new int[5]
    int[] a=new int[5]{23,23,23,1,2,1}
    int[] a=new int[]{45,23,34}会把前三个值赋值,后两个元素保持默认值0.
    int[] a=new int[]{34,234,5,3,4,23};

    取值:
    数组名[下标值]

    数组的优点:
    1.对于大量数据来说,保存的时候,定义一个数组即可解决。
    2.用循环来控制数组的下标,可以对数组进行批量操作。
    eg.
    int[] a=new int[5];
    //数组的批量赋值
    for(int i=0;i<5;i++)
    {
    a[i]=(i+1)*4;
    }
    //数组的批量取值
    for(int j=0;j<5;j++)
    {
    console.writeline(a[j]);
    }

    案例一:做一个教练为10个球员打分的程序。

     1  //eg.7  球员成绩打分
     2         static void Main7(string[] args)
     3         {
     4             int[] a=new int[10];
     5             int sum = 0;
     6             double aver = 0.0;
     7             //Console.WriteLine("请输入球员成绩的总个数:");
     8             //int n=int.Parse(Console.ReadLine());
     9 
    10             for (int i = 0; i < a.Length; i++)
    11             {
    12                 Console.WriteLine("请输入第"+(i+1)+"个球员的成绩:");
    13                 a[i] = Convert.ToInt32(Console.ReadLine());
    14             }
    15 
    16             for (int i = 1; i <= a.Length - 1; i++)
    17             {
    18                 for (int j = 1; j <= a.Length - i; j++)
    19                 {
    20                     if (a[j - 1] < a[j])
    21                     {
    22                         int t = a[j - 1];
    23                         a[j - 1] = a[j];
    24                         a[j] = t;
    25                     }
    26                 }
    27             }
    28 
    29             foreach (int e in a)
    30             {
    31                 Console.Write(e+"	");
    32             }
    33 
    34             for (int i = 2; i < a.Length - 2; i++)
    35             {
    36                 sum += a[i];
    37             }
    38 
    39             aver = sum / (a.Length - 4);
    40 
    41             Console.WriteLine("该选手的平均成绩是{0}。",aver);
    42         }

    数组的应用
    一、冒泡排序
    1.冒泡排序是用双层循环解决。外层循环的是趟数,里层循环的是次数。
    2.趟数=n-1;次数=n-趟数。
    3.里层循环使用if比较相邻的两个数的大小。进行数值交换。

    程序如下

     1 static void Main2(string[] args)
     2         {
     3             int[] a = new int[8] { 9, 12, 7, 5, 15, 2, 1, 8 };
     4             for (int i = 1; i <= a.Length - 1; i++)//趟数
     5             {
     6                 for (int j = 1; j <= a.Length - i - 1; j++)//次数, 趟数+次数==数组个数,a.Length - i,每趟都有沉到底的一个不用再排
     7                 {
     8                     if (a[j - 1] < a[j])
     9                     {
    10                         int t = a[j - 1];
    11                         a[j - 1] = a[j];
    12                         a[j] = t;
    13                     }
    14                 }
    15             }
    16             //显示
    17             for (int k = 0; k < a.Length; k++)
    18             {
    19                 Console.WriteLine(a[k]);
    20             }
    21             Console.ReadKey();
    22         }

    二、折半查找
    前提:数组必须是有序的
    思路:用两个变量分别代表上限(top)和下限(bottom)的下标,再用一个变量代表中间的下标(mid)。
    1.求中间的下标:mid=(top+bottom)/2
    2.上限下标下移:top=mid+1.假设数组是升序排列。
    3.下限下标上移:bottom=mid-1.
    4.循环条件是:bottom>=top

    程序如下:

     1 //折半查找
     2         static void Main3(string[] args)
     3         {
     4             int[] d = new int[] { 3, 5, 7, 9, 11, 13, 14, 18 };
     5             Console.Write("请输入要查找的数字:");
     6             int find = Convert.ToInt32(Console.ReadLine());
     7             int top = 0;
     8             int bottom = d.Length - 1;
     9             int mid;
    10             while (top <= bottom)
    11             {
    12                 //取中间的坐标
    13                 mid = (bottom + top) / 2;
    14                 //取中间的值
    15                 int n = d[mid];
    16                 if (find > n)
    17                 {
    18                     top = mid + 1;
    19                 }
    20                 else if (find < n)
    21                 {
    22                     bottom = mid - 1;
    23                 }
    24                 else
    25                 {
    26                     Console.WriteLine("你找到了该数字,在第" + (mid + 1) + "");
    27                     break;
    28                 }
    29             }
    30             Console.ReadKey();
    31         }

    二维数组

    eg.1推箱子

      1 //eg.6 推箱子 自己做
      2         static void Main6a(string[] args)
      3         {
      4             //定义地图
      5                 #region 定义地图,初始化坐标
      6             int[,] a = new int[10, 10]{
      7             {8,8,8,8,8,8,8,8,8,8},
      8             {8,0,0,0,8,0,0,0,0,8},
      9             {8,0,2,0,8,8,8,8,0,8},
     10             {8,0,0,0,0,0,0,8,0,8},
     11             {8,1,0,0,0,0,0,8,0,8},
     12             {8,0,0,0,8,0,0,0,0,8},
     13             {8,0,0,0,8,0,0,0,0,8},
     14             {8,0,0,0,8,0,0,0,0,8},
     15             {8,0,0,0,8,0,0,0,3,8},
     16             {8,8,8,8,8,8,8,8,8,8}};
     17             int x, y, t, m, n, k;
     18             x = 4; y = 1;
     19             m = 2; n = 2;
     20 #endregion
     21 
     22                 #region 显示地图
     23             //把地图显示,必须显示地图,否则会有等待按键的延时!
     24             Console.Clear();//清屏
     25             for (int i = 0; i < 10; i++)
     26             {
     27                 for (int j = 0; j < 10; j++)
     28                 {
     29                     //Console.ForegroundColor = ConsoleColor.White;
     30 
     31                     if (a[i, j] == 1)
     32                         Console.Write("");
     33                     else if (a[i, j] == 0)
     34                         Console.Write("  ");
     35                     else if (a[i, j] == 3)
     36                     {
     37                         //Console.ForegroundColor = ConsoleColor.Red;
     38                         Console.Write("");
     39                         //    Console.ForegroundColor = ConsoleColor.White;
     40                     }
     41                     else if (a[i, j] == 8)
     42                         Console.Write("");
     43                     else
     44                         Console.Write("");
     45                 }
     46                 Console.WriteLine();
     47             }
     48             #endregion
     49 
     50             while (a[8,8]!=2)
     51             {
     52                
     53                 ConsoleKeyInfo s = Console.ReadKey();
     54 
     55                 #region 向右移动
     56                 if ((s.Key.ToString() == "RightArrow"))
     57                 { 
     58                     t = a[x, y];
     59                     k = a[m, n];
     60 
     61                     //小人走的下一步有3种情况:是不是墙?是不是箱子?是不是终点?
     62                     if (a[x, y + 1] != 8 && a[x, y + 1] != 2 && a[x, y + 1] != 3)
     63                     {
     64                         a[x, y] = a[x, y + 1];
     65                         a[x, y + 1] = t;
     66                         y++;
     67                     }
     68 
     69                     else if (a[x, y + 1] == 2)//小人的下一步如果是箱子的话
     70                     {
     71                         if (a[m, n + 1] != 8 && a[m, n + 1] != 3)//箱子的下一步只有墙,终点 这两个分支
     72                         {
     73                             //箱子移动后的改变
     74                             a[m, n] = a[m, n + 1];
     75                             a[m, n + 1] = k;
     76                             n++;
     77 
     78                             //人移动后的替换
     79                             a[x, y] = a[x, y + 1];
     80                             a[x, y + 1] = t;
     81                             y++;
     82                         }
     83                         else if (a[m, n + 1] == 8)//箱子的下一步是墙,空操作,不动
     84                         {
     85                             Console.WriteLine("a");
     86                         }
     87                         else if (a[m, n + 1] == 3)//箱子的下一步 是 终点
     88                         {
     89                             a[x, y + 2] = 2;
     90                             a[x, y + 1] = 1;
     91                             a[x, y] = 0;
     92                             //y++;
     93                             //m++;
     94                         }
     95 
     96                     }
     97       
     98                     else if (a[x, y + 1] == 8 || a[x, y + 1] == 3)//小人的下一步如果是墙或者终点的话
     99                     {
    100                         Console.WriteLine("a");
    101                     }
    102                 }
    103                 #endregion
    104 
    105                 #region 向上移动
    106                 else if ((s.Key.ToString() == "UpArrow"))
    107                 {
    108                     t = a[x, y];
    109                     k = a[m, n];
    110 
    111                     //小人走的下一步有3种情况:是不是墙?是不是箱子?是不是终点?
    112                     if (a[x - 1, y] != 8 && a[x - 1, y] != 2 && a[x - 1, y] != 3)
    113                     {
    114                         a[x, y] = a[x - 1, y];
    115                         a[x - 1, y] = t;
    116                         x--;
    117                     }
    118 
    119                     else if (a[x - 1, y] == 2)//小人的下一步如果是箱子的话
    120                     {
    121                         if (a[m - 1, n] != 8 && a[m - 1, n] != 3)//箱子的下一步只有墙,终点 这两个分支
    122                         {
    123                             //箱子移动后的改变
    124                             a[m, n] = a[m - 1, n];
    125                             a[m - 1, n] = k;
    126                             m--;
    127 
    128                             //人移动后的替换
    129                             a[x, y] = a[x - 1, y];
    130                             a[x - 1, y] = t;
    131                             x--;
    132                         }
    133                         else if (a[m - 1, n] == 8)
    134                         {
    135 
    136                         }
    137                         else if (a[m - 1, n] == 3)
    138                         {
    139                             a[m, n] = a[x - 1, y] = 1;
    140                             a[m - 1, n] = 2;
    141                             a[x, y] = 0;
    142                             m--;
    143                             y--;
    144                         }
    145                     }
    146 
    147                     else if (a[x - 1, y] == 8 || a[x - 1, y] == 3)//小人的下一步如果是墙或者终点的话
    148                     {
    149 
    150                     }
    151                 }
    152                 #endregion
    153 
    154                 #region 向下移动
    155                 else if ((s.Key.ToString() == "DownArrow"))
    156                 {
    157                     t = a[x, y];
    158                     k = a[m, n];
    159 
    160                     //小人走的下一步有3种情况:是不是墙?是不是箱子?是不是终点?
    161                     if (a[x+1, y] != 8 && a[x+1, y] != 2 && a[x+1, y] != 3)
    162                     {
    163                         a[x, y] = a[x+1, y];
    164                         a[x+1, y] = t;
    165                         x++;
    166                     }
    167 
    168                     else if (a[x + 1, y] == 2)//小人的下一步如果是箱子的话
    169                     {
    170                         if (a[m + 1, n] != 8 && a[m + 1, n] != 3)//箱子的下一步只有墙,终点 这两个分支
    171                         {
    172                             //箱子移动后的改变
    173                             a[m, n] = a[m + 1, n];
    174                             a[m + 1, n] = k;
    175                             m++;
    176 
    177                             //人移动后的替换
    178                             a[x, y] = a[x + 1, y];
    179                             a[x + 1, y] = t;
    180                             x++;
    181                         }
    182                         else if (a[m + 1, n] == 8)
    183                         {
    184 
    185                         }
    186                         else if (a[m + 1, n] == 3)
    187                         {
    188                             a[m, n] = a[x+1, y] = 1;
    189                             a[m+1, n] = 2;
    190                             a[x, y] = 0;
    191                             m++;
    192                             y++;
    193                         }
    194                     }
    195 
    196                     else if (a[x + 1, y] == 8 || a[x + 1, y] == 3)//小人的下一步如果是墙或者终点的话
    197                     {
    198 
    199                     }
    200                 }
    201                 #endregion
    202 
    203                 #region 向左移动
    204                 else if ((s.Key.ToString() == "LeftArrow"))
    205                 {
    206                     t = a[x, y];
    207                     k = a[m, n];
    208 
    209                     //小人走的下一步有3种情况:是不是墙?是不是箱子?是不是终点?
    210                     if (a[x, y - 1] != 8 && a[x, y - 1] != 2 && a[x, y - 1] != 3)
    211                     {
    212                         a[x, y] = a[x, y - 1];
    213                         a[x, y - 1] = t;
    214                         y--;
    215                     }
    216 
    217                     else if (a[x, y - 1] == 2)//小人的下一步如果是箱子的话
    218                     {
    219                         if (a[m, n - 1] != 8 && a[m, n - 1] != 3)//箱子的下一步只有墙,终点 这两个分支
    220                         {
    221                             //箱子移动后的改变
    222                             a[m, n] = a[m, n - 1];
    223                             a[m, n - 1] = k;
    224                             n--;
    225 
    226                             //人移动后的替换
    227                             a[x, y] = a[x, y - 1];
    228                             a[x, y - 1] = t;
    229                             y--;
    230                         }
    231                         else if (a[m, n - 1] == 8)
    232                         {
    233 
    234                         }
    235                         else if (a[m, n - 1] == 3)
    236                         {
    237                             a[m, n] = a[x, y - 1] = 1;
    238                             a[m, n - 1] = 2;
    239                             a[x, y] = 0;
    240                             n--;
    241                             y--;
    242                         }
    243                     }
    244 
    245                     else if (a[x, y - 1] == 8 || a[x, y - 1] == 3)//小人的下一步如果是墙或者终点的话
    246                     {
    247 
    248                     }
    249                 }
    250                 #endregion
    251 
    252                 #region 显示地图
    253                 //把地图显示,必须显示地图,否则会有等待按键的延时!
    254                 Console.Clear();//清屏
    255                 for (int i = 0; i < 10; i++)
    256                 {
    257                     for (int j = 0; j < 10; j++)
    258                     {
    259                         //Console.ForegroundColor = ConsoleColor.White;
    260 
    261                         if (a[i, j] == 1)
    262                             Console.Write("");
    263                         else if (a[i, j] == 0)
    264                             Console.Write("  ");
    265                         else if (a[i, j] == 3)
    266                         {
    267                             //Console.ForegroundColor = ConsoleColor.Red;
    268                             Console.Write("");
    269                             //    Console.ForegroundColor = ConsoleColor.White;
    270                         }
    271                         else if (a[i, j] == 8)
    272                             Console.Write("");
    273                         else
    274                             Console.Write("");
    275                     }
    276                     Console.WriteLine();
    277                 }
    278                 #endregion
    279             }
    280         }
    281         //eg.6 推箱子 老师版  
    282         static void Main6b(string[] args)
    283         {
    284             #region 定义地图,初始化坐标
    285             int[,] a = new int[10, 10]{
    286             {8,8,8,8,8,8,8,8,8,8},
    287             {8,0,0,0,8,0,0,0,0,8},
    288             {8,0,2,0,8,8,8,8,0,8},
    289             {8,0,0,0,0,0,0,8,0,8},
    290             {8,1,2,0,0,0,0,8,0,8},
    291             {8,0,0,0,8,0,0,0,0,8},
    292             {8,0,2,0,8,0,0,0,0,8},
    293             {8,0,3,0,8,0,0,0,0,8},
    294             {8,0,0,0,8,0,3,0,3,8},
    295             {8,8,8,8,8,8,8,8,8,8}};
    296 
    297             int x = 4, y = 1;//定义坐标的时候不用定义多个,可以用x,y与之关联性相加或相减
    298             #endregion
    299 
    300             #region 显示地图
    301             //把地图显示,必须显示地图,否则会有等待按键的延时!
    302             //Console.Clear();//清屏
    303             for (int i = 0; i < 10; i++)
    304             {
    305                 for (int j = 0; j < 10; j++)
    306                 {
    307                     //Console.ForegroundColor = ConsoleColor.White;
    308 
    309                     if (a[i, j] == 1)
    310                         Console.Write("");
    311                     else if (a[i, j] == 0)
    312                         Console.Write("  ");
    313                     else if (a[i, j] == 3)
    314                     {
    315                         //Console.ForegroundColor = ConsoleColor.Red;
    316                         Console.Write("");
    317                         //    Console.ForegroundColor = ConsoleColor.White;
    318                     }
    319                     else if (a[i, j] == 8)
    320                         Console.Write("");
    321                     else
    322                         Console.Write("");
    323                 }
    324                 Console.WriteLine();
    325             }
    326             #endregion
    327 
    328             while (a[8, 8] != 2 || a[7, 2] != 2 || a[8, 6] != 2)
    329             {
    330                 ConsoleKeyInfo key = Console.ReadKey();
    331 
    332                 #region 向右走
    333                 if (key.Key.ToString() == "RightArrow")
    334                 {
    335                     if (y < 8)//右移最大不会超过y=8坐标
    336                     {
    337                         if (a[x, y + 1] == 0)//人的下一步只能是空地,或箱子,或墙,或终点,先判断空地,老师选择是==情况,我选的是!=情况,区别
    338                         {
    339                             a[x, y + 1] = 1;
    340                             a[x, y] = 0;
    341                             //走过终点后,再让终点恢复出来!
    342                             if (a[8, 8] != 2)
    343                                 a[8, 8] = 3;
    344                             if (a[7, 2] != 2)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    345                                 a[7, 2] = 3;
    346                             if (a[8, 6] != 2)
    347                                 a[8, 6] = 3;
    348                             y++;
    349                             
    350                         }
    351                         else if (a[x, y + 1] == 3)//人的下一步是终点
    352                         {
    353                             a[x, y + 1] = 1;
    354                             a[x, y] = 0;
    355                             y++;
    356                         }
    357                         else if (a[x, y + 1] == 2 && a[x, y + 2] == 0)//人的下一步是箱子,箱子的下一步是空地
    358                         {
    359                             a[x, y + 1] = 1;
    360                             a[x, y + 2] = 2;
    361                             a[x, y] = 0;
    362 
    363                             if (a[8, 8] != 2 && a[8, 8] != 1)
    364                                 a[8, 8] = 3;
    365                             if (a[7, 2] != 2 && a[7, 2] != 1)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    366                                 a[7, 2] = 3;
    367                             if (a[8, 6] != 2 && a[8, 6] != 1)
    368                                 a[8, 6] = 3;
    369 
    370                             y++;
    371                         }
    372                         else if (a[x, y + 1] == 2 && a[x, y + 2] == 3)//人的下一步是箱子,箱子的下一步是终点
    373                         {
    374                             a[x, y + 1] = 1;
    375                             a[x, y + 2] = 2;
    376                             a[x, y] = 0;
    377 
    378                             if (a[8, 8] != 2)
    379                                 a[8, 8] = 3;
    380                             if (a[7, 2] != 2)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    381                                 a[7, 2] = 3;
    382                             if (a[8, 6] != 2)
    383                                 a[8, 6] = 3;
    384 
    385                             y++;
    386                         }
    387                         else //其中包括,人的下一步是墙,箱子的下一步是墙
    388                             Console.WriteLine("a");
    389                     }
    390                     else
    391                         Console.WriteLine("a");
    392                 }
    393                 #endregion 
    394 
    395                 #region 向左走
    396                 if (key.Key.ToString() == "LeftArrow")
    397                 {
    398                     if (y > 1)//左移最小不会小于y=1坐标
    399                     {
    400                         if (a[x, y - 1] == 0)//人的下一步只能是空地,或箱子,或墙,先判断空地
    401                         {
    402                             a[x, y - 1] = 1;
    403                             a[x, y] = 0;
    404 
    405                             if (a[8, 8] != 2)
    406                                 a[8, 8] = 3;
    407                             if (a[7, 2] != 2)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    408                                 a[7, 2] = 3;
    409                             if (a[8, 6] != 2)
    410                                 a[8, 6] = 3;
    411                             y--;
    412                         }
    413                         else if (a[x, y - 1] == 3)
    414                         {
    415                             a[x, y - 1] = 1;
    416                             a[x, y] = 0;
    417                             y--;
    418                         }
    419                         else if (a[x, y - 1] == 2 && a[x, y - 2] == 0)//人的下一步是箱子
    420                         {
    421                             a[x, y - 1] = 1;
    422                             a[x, y - 2] = 2;
    423                             a[x, y] = 0;
    424                             y--;
    425                         }
    426                         else if (a[x, y - 1] == 2 && a[x, y - 2] == 3)//人的下一步是箱子,箱子的下一步是终点
    427                         {
    428                             a[x, y - 1] = 1;
    429                             a[x, y - 2] = 2;
    430                             a[x, y] = 0;
    431                             y--;
    432                         }
    433                         else //其中包括,人的下一步是墙,箱子的下一步是墙
    434                             Console.WriteLine("a");
    435                     }
    436                     else
    437                         Console.WriteLine("a");
    438                 }
    439                 #endregion
    440 
    441                 #region 向上走
    442                 if (key.Key.ToString() == "UpArrow")
    443                 {
    444                     if (x > 1)//上移不能超过x=8坐标
    445                     {
    446                         if (a[x - 1, y] == 0)//人的下一步只能是空地,或箱子,或墙,先判断空地
    447                         {
    448                             a[x - 1, y] = 1;
    449                             a[x, y] = 0;
    450 
    451                             if (a[8, 8] != 2)
    452                                 a[8, 8] = 3;
    453                             if (a[7, 2] != 2)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    454                                 a[7, 2] = 3;
    455                             if (a[8, 6] != 2)
    456                                 a[8, 6] = 3;
    457 
    458                             x--;
    459                         }
    460                         else if (a[x - 1, y] == 3)//人下一步是终点,走上去的时候照样替换,走之后,就是人下一步是空地的问题了
    461                         {
    462                             a[x - 1, y] = 1;
    463                             a[x, y] = 0;
    464                             x--;
    465                         }
    466                         else if (a[x - 1, y] == 2 && a[x - 2, y] == 0)//人的下一步是箱子,箱子的下一步是空地
    467                         {
    468                             a[x - 1, y] = 1;
    469                             a[x - 2, y] = 2;
    470                             a[x, y] = 0;
    471 
    472                             if (a[8, 8] != 2&&a[8, 8] != 1)
    473                                 a[8, 8] = 3;
    474                             if (a[7, 2] != 2&&a[7, 2] != 1)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    475                                 a[7, 2] = 3;
    476                             if (a[8, 6] != 2&&a[8, 6] != 1)
    477                                 a[8, 6] = 3;
    478 
    479                             x--;
    480                         }
    481                         else if (a[x - 1, y] == 2 && a[x - 2, y] == 3)//人的下一步是箱子,箱子的下一步是终点
    482                         {
    483                             a[x - 1, y] = 1;
    484                             a[x - 2, y] = 2;
    485                             a[x, y] = 0;
    486 
    487                             if (a[8, 8] != 2)
    488                                 a[8, 8] = 3;
    489                             if (a[7, 2] != 2)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    490                                 a[7, 2] = 3;
    491                             if (a[8, 6] != 2)
    492                                 a[8, 6] = 3;
    493 
    494                             x--;
    495                         }
    496                         else //其中包括,人的下一步是墙,箱子的下一步是墙
    497                             Console.WriteLine("a");
    498                     }
    499                     else
    500                         Console.WriteLine("a");
    501                 }
    502                 #endregion
    503 
    504                 #region 向下走
    505                 if (key.Key.ToString() == "DownArrow")
    506                 {
    507                     if (x < 8)//下移不能超过x=8坐标,只能下走增大,给个上限就可以
    508                     {
    509                         if (a[x + 1, y] == 0)//人的下一步只能是空地,或箱子,或墙,先判断空地
    510                         {
    511                             a[x + 1, y] = 1;
    512                             a[x, y] = 0;
    513                             if (a[8, 8] != 2)
    514                                 a[8, 8] = 3;
    515                             if (a[7, 2] != 2)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    516                                 a[7, 2] = 3;
    517                             if(a[8, 6] != 2)
    518                                 a[8, 6] = 3;
    519                             x++;
    520                         }
    521                         else if (a[x + 1, y] == 3)//解决人不能通过终点坐标问题
    522                         {
    523                             a[x + 1, y] = 1;
    524                             a[x, y] = 0;
    525                             x++;
    526                         }
    527                         else if (a[x + 1, y] == 2 && a[x + 2, y] == 0)//人的下一步是箱子,箱子的下一步是空地
    528                         {
    529                             a[x + 1, y] = 1;
    530                             a[x + 2, y] = 2;
    531                             a[x, y] = 0;
    532 
    533                             if (a[8, 8] != 2&&a[8, 8] != 1)
    534                                 a[8, 8] = 3;
    535                             if (a[7, 2] != 2&&a[7, 2] != 1)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    536                                 a[7, 2] = 3;
    537                             if (a[8, 6] != 2&&a[8, 6] != 1)
    538                                 a[8, 6] = 3;
    539 
    540                             x++;
    541                         }
    542                         else if (a[x + 1, y] == 2 && a[x + 2, y] == 3)//人的下一步是箱子,箱子的下一步是终点
    543                         {
    544                             a[x + 1, y] = 1;
    545                             a[x + 2, y] = 2;
    546                             a[x, y] = 0;
    547 
    548                             if (a[8, 8] != 2)
    549                                 a[8, 8] = 3;
    550                             if (a[7, 2] != 2)//当箱子推入终点后,如果人的下一步是空地的话,此时就不能让终点的坐标还显示,而是显示箱子
    551                                 a[7, 2] = 3;
    552                             if (a[8, 6] != 2)
    553                                 a[8, 6] = 3;
    554 
    555                             x++;
    556                         }
    557                         else //其中包括,人的下一步是墙,箱子的下一步是墙
    558                             Console.WriteLine("a");
    559                     }
    560                     else
    561                         Console.WriteLine("a");
    562                 }
    563                 #endregion
    564                                                                                                                                                                                   
    565                 #region 重新显示地图(按键之后的地图)
    566                 //把地图显示,必须显示地图,否则会有等待按键的延时!
    567                 Console.Clear();//清屏
    568                 for (int i = 0; i < 10; i++)
    569                 {
    570                     for (int j = 0; j < 10; j++)
    571                     {
    572                         //Console.ForegroundColor = ConsoleColor.White;
    573 
    574                         if (a[i, j] == 1)
    575                             Console.Write("");
    576                         else if (a[i, j] == 0)
    577                             Console.Write("  ");
    578                         else if (a[i, j] == 3)
    579                         {
    580                             //Console.ForegroundColor = ConsoleColor.Red;
    581                             Console.Write("");
    582                             //    Console.ForegroundColor = ConsoleColor.White;
    583                         }
    584                         else if (a[i, j] == 8)
    585                             Console.Write("");
    586                         else
    587                             Console.Write("");
    588                     }
    589                     Console.WriteLine();
    590                 }
    591                 #endregion
    592             }
    593             Console.WriteLine("恭喜你过关了!");
    594             Console.ReadLine();
    595         }

    eg.2 抽手机号中奖问题

     1   //eg.8  抽奖游戏
     2         static void Main8(string[] args)
     3         {
     4             Console.WriteLine("请输入要抽奖的手机号的个数:");
     5             int a = int.Parse(Console.ReadLine());
     6 
     7             string[] b=new string[a];
     8 
     9             for (int i = 0; i < b.Length; i++)
    10             {
    11                 Console.WriteLine("请输入第"+(i+1)+"个手机号:");
    12                 b[i] = Console.ReadLine();
    13             }
    14 
    15             DateTime dt = DateTime.Now;
    16             dt = dt.AddSeconds(5);
    17             while (true)
    18             {
    19                 for (int i = 0; i < b.Length;i++ )
    20                 {
    21                     Console.Clear();
    22                     Console.WriteLine(b[i]);
    23                     Thread.Sleep(50);
    24 
    25                     //break 跳出的只是for 不是 while 循环,错误
    26                     //if (dt.ToString("yyyy/MM/ddHHmmss") == DateTime.Now.ToString("yyyy/MM/ddHHmmss"))
    27                     //{
    28                     //    //Console.WriteLine("18753351659");
    29                     //    break;
    30                     //}
    31                     //else
    32                     //{
    33 
    34                     //}
    35                     
    36                     
    37                 }
    38 
    39                 if (dt.ToString() == DateTime.Now.ToString())
    40                 {
    41                     //Console.WriteLine("18753351659");
    42                     break;
    43                 }
    44                 else
    45                 {
    46 
    47                 }
    48             }
    49             //Console.Clear(); 作弊方法
    50             //Console.WriteLine("18753351639");
    51             Console.ReadLine();
    52         }

    eg.3 红篮球摇号问题

     1  //eg.9 摇号 蓝球,红球
     2         static void Main9(string[] args)
     3         {
     4             //int lan = 0;
     5             string[] a = new string[8];
     6             Random r = new Random();
     7 
     8             //a[0] =Convert.ToString(r.Next(17));
     9             //a[1] = " ";
    10 
    11             int i = 2;
    12             for ( ; i<= a.Length - 1; )
    13             {
    14                 int b = r.Next(34);
    15                 //if(a.Contains(a[i])),这样就永远返回true类型了
    16                 if (a.Contains(b.ToString()))//也可用(!=)i--;来做
    17                 {
    18                     continue;
    19                 }
    20                 else
    21                 {
    22                     a[i] = b.ToString();
    23                     i++;
    24                 }
    25             }
    26 
    27             a[0] = Convert.ToString(r.Next(17));
    28             a[1] = " ";
    29 
    30             foreach (string j in a)
    31             {
    32                 Console.Write(j+"	");
    33             }
    34         }

    eg.4 投票选举问题(下标的选择 也可由投票的号码来定)

     1 //eg.5-1 投票 0--代表第一个人候选人,1代表第二个候选人。。。。共5个人(老师版)
     2         static void Main51(string[] args)
     3         {
     4             //30
     5             int[] vote = new int[5];
     6             for (int i = 0; i < 20; i++)
     7             {
     8                 Console.Write("请第" + (i + 1) + "位同学投票(0-4):");
     9                 int temp = Convert.ToInt32(Console.ReadLine());
    10                 if (temp < 0 || temp > 4)
    11                 {
    12                     Console.WriteLine("废票");
    13                     continue;
    14                 }
    15                 else
    16                 {
    17                     vote[temp]++;
    18                 }
    19             }
    20 
    21             //计算最终得票
    22             int max = 0, maxSub = 0;
    23             for (int i = 0; i < vote.Length; i++)
    24             {
    25                 //把每位候选人的票数显示出来。
    26                 Console.WriteLine("" + (i + 1) + "号候选人的票数是" + vote[i]);
    27                 //计算谁得票最多
    28                 if (vote[i] > max)
    29                 {
    30                     max = vote[i];
    31                     maxSub = i;
    32                 }
    33             }
    34             //显示最终结果
    35             Console.WriteLine("最终投票结果为:" + maxSub + "号候选人当选,当选的票数是" + max + "票。");
    36             Console.ReadKey();
    37         }
    38         //eg.5-2 投票 1--代表第一个人候选人,2代表第二个候选人。。。。共5个人
    39         static void Main52(string[] args)
    40         {
    41             int[] ren = new int[20];
    42             int[] vote = new int[5];
    43 
    44             for (int i = 0; i <= ren.Length - 1; i++)
    45             {
    46                 Console.WriteLine("请第" + (i + 1) + "公民投票:");
    47                 ren[i] = int.Parse(Console.ReadLine());
    48                 if (ren[i] > 5 || ren[i] < 0)
    49                 {
    50                     Console.WriteLine("废票,请重投票!");
    51                     i--;
    52                     continue;
    53                 }
    54                 else
    55                 {
    56                     vote[ren[i] - 1]++;
    57                 }
    58             }
    59 
    60             int max = vote[0];
    61             for (int i = 0; i < vote.Length; i++)
    62             {
    63                 if (max <= vote[i])
    64                 {
    65                     max = vote[i];
    66                 }
    67             }
    68             Console.WriteLine("最高票数是{0}票", max);
    69 
    70             for (int i = 0; i < vote.Length; i++)
    71             {
    72                 if (max == vote[i])
    73                 {
    74                     //int y = i + 1;
    75                     Console.WriteLine("候选人是第{0}位选手。", i + 1);
    76                 }
    77             }
    78         }
  • 相关阅读:
    angularjs下载地址
    Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of
    java.net.SocketException: No buffer space available 异常
    在ubuntu上安装svn+apache2
    MyBatis配置
    npm被墙解决方法
    XMPP_05_网络编程
    XMPP_04_环境安装(配置客户端)
    XMPP_03_环境安装(配置服务器)
    XMPP_02_环境安装(准备工作和配置数据库)
  • 原文地址:https://www.cnblogs.com/zyh-club/p/4631711.html
Copyright © 2011-2022 走看看