• 数组的应用


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

    作业:
    1.先把冒泡排序写一遍。

                int[] a = new int[8] { 9, 12, 7, 5, 15, 2, 1, 8 };
                //冒泡排序。
                for(int i=1;i<=a.Length-1;i++) //趟数
                {
                    for (int j = 1; j <= a.Length - i; j++)//次数
                    {
                        if(a[j-1] > a[j])
                        {
                            int t = a[j - 1];
                            a[j - 1] = a[j];
                            a[j] = t;
                        }
                    }
                }
    
                //显示
                for(int k=0;k<a.Length;k++)
                {
                    Console.WriteLine(a[k]);
                }

    2.使用冒泡排序,做青歌赛的打分程序。要求去掉两个最高,两个最低分,求平均得分。

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

                int[] a = new int[] { 3, 5, 7, 9, 11, 13, 14, 18 };
    
                Console.Write("请输入要找的数:");
                int find = Convert.ToInt32(Console.ReadLine());
    
                int top, bottom, mid;  //上限下标,下限下标,中间下标
                top = 0;
                bottom = a.Length - 1;
                
                while(bottom>=top)  //只要下限下标还在上限下标的下面,就循环,否则没找到就结束。
                {
                    //算中间下标
                    mid = (top + bottom) / 2;
                    //取中间的值
                    int n = a[mid];
                    if(n < find)
                    {
                        top = mid + 1;      //调整上限的下标
                    }
                    else if(n>find)
                    {
                        bottom = mid - 1;// 调整下限的下标。
                    }
                    else
                    {
                        Console.WriteLine("找到了,在第" + mid + "个元素上");
                        break;
                    }
                }

    二维数组:
    表格的模型。
    定义:
     数据类型[,] 数组名 = new 数组类型[维度长度,维度长度];
     int[,] a = new int[3,4];
     int[,] a = new int[3, 4] { { 1, 2, 3, 4 },{ 5, 6, 7, 8 }, { 9, 0, 9, 8 } };
    赋值:
     数组名[下标,下标] = 值;
     a[0,0] = 5;
     a[2,3] = 10;
    取值:
     数组名[下标,下标];

    应用:

    //推箱子地图

                int[,] map = new int[10, 10]
                {
                    {1,1,1,1,1,1,1,1,1,1},
                    {1,0,0,0,0,1,0,0,0,1},
                    {1,8,0,0,0,1,0,0,0,1},
                    {1,0,0,0,0,1,1,1,0,1},
                    {1,0,0,0,0,0,0,1,0,1},
                    {1,0,0,0,1,0,0,1,0,1},
                    {1,0,0,0,1,0,0,0,0,1},
                    {1,1,1,1,1,0,0,0,0,1},
                    {1,0,0,0,0,0,0,0,9,1},
                    {1,1,1,1,1,1,1,1,1,1}
                };
    
                    for (int i = 0; i < 10; i++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            if (map[i, j] == 1)
                            {
                                Console.Write("");
                            }
                            else if (map[i, j] == 0)
                            {
                                Console.Write("  ");
                            }
                            else if (map[i, j] == 2)
                            {
                                Console.Write("");
                            }
                            else if (map[i, j] == 9)
                            {
                                Console.Write("");
                            }
                            else if (map[i, j] == 8)
                            {
                                Console.Write("");
                            }
                        }
                        Console.WriteLine();
                    }
  • 相关阅读:
    !JS实战之随机像素图
    bgp选路原则【第二部】
    BGP基础【第三部】
    【★】KMP算法完整教程
    ★如何引导客户需求?几个经…
    html标签缺省(自带)样式大全
    Web颜色对照表大全
    PS各个工具的字母快捷键和英…
    色相、明度及饱和度
    用webgl打造自己的3D迷宫游戏
  • 原文地址:https://www.cnblogs.com/691331894w/p/4190350.html
Copyright © 2020-2023  润新知