• C#数组的应用:冒泡排序,折半查找及二维数组的应用


    C#数组的应用:冒泡排序,折半查找及二维数组的应用

    人类思维--计算机逻辑思维
    逻辑思维--代码实现

    写书法:
    描红——临摹——碑贴——自成一体——草

    复习:
    数组:一维,二维,多维
    一维:豆角。连续,同一类型。
    定义:数据类型[] 数组名=new 数据类型[长度]{.,.,.,.};
    赋值:数组名[下标] = 值
    取值:数组名[下标]
    灵活运用:与for循环的结合应用。
    1.求最大值,最小值。
    2.求总和,平均。
    3.随机(生成下标)抽值。

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

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

    int[] a = new int[] { 23, 2, 5, 12, 55, 4, 6, 45, 34, };
    for (int i = 1; i <= a.Length; i++)//第一次循环趟数为数组长度-1
    {
    for (int j = 1; j <= a.Length - i; j++)//第二层循环次数为数组长度-循环趟数
    {
    if (a[j] > a[j - 1])//判断两个相邻数组元素的大小
    {
    int m = a[j - 1];//将两个数组元素的值调换
    a[j - 1] = a[j];
    a[j] = m;
    }
    }
    }
    for (int i = 0; i < a.Length; i++)//输出数组元素
    {
    Console.WriteLine(a[i]);
    }


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

    代码。

    Console.WriteLine("请评委老师打分");
    int[] a = new int[10];
    for (int i = 0; i < a.Length; i++)
    {
    a[i] = Convert.ToInt32(Console.ReadLine());
    }
    for (int i = 1; i <= a.Length - 1; i++)//第一次循环趟数为数组长度-1
    {
    for (int j = 1; j <= a.Length - i; j++)//第二层循环次数为数组长度-循环趟数
    {
    if (a[j] > a[j - 1])
    {
    int m = a[j];//将两个数组元素的值调换
    a[j] = a[j - 1];
    a[j - 1] = m;
    }
    }
    }
    int sum = 0;
    Console.WriteLine("选手得分为");
    for (int i = 0; i < a.Length; i++)//输出选手的得分
    {
    Console.Write(a[i] + " ");
    }

    for (int i = 2; i < a.Length - 2; i++)//计算去掉两个最高分和最低分后的总分
    {
    sum = sum + a[i];
    }
    double avg = 1.0 * sum / (a.Length - 4);//计算去掉两个最高分和最低分后的平均分
    Console.WriteLine("选手得分去掉两个最高分和两个最低分最后得分为" + avg + "分。");

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

    例题:

    int[] a = new int[] { 3, 4, 5, 7, 8, 12, 15, 17 };
    Console.Write("请输入数值");
    int n = Convert.ToInt32(Console.ReadLine());
    int top = 0, bottom = a.Length - 1, mid;
    while (top <= bottom)
    {
    mid = (top + bottom) / 2;
    if (n > a[mid])
    {
    top = mid + 1;
    }
    else if (n < a[mid])
    {
    bottom = mid - 1;

    }
    else
    {
    Console.WriteLine("找到了,在第" + (mid + 1) + "个元素上");
    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 x=3, y=1;//记录小人初始位置
    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,0,0,0,0,1,0,0,0,1},
    {1,4,2,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,3,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] == 0)
    {
    Console.Write(" ");
    }
    else if (map[i, j] == 1)
    {
    Console.Write("■");
    }
    else if (map[i, j] == 2)
    {
    Console.Write("□");
    }
    else if (map[i, j] == 3)
    {
    Console.Write("★");
    }
    else if (map[i, j] == 4)
    {
    Console.Write("♀");
    }
    }

    Console.WriteLine();
    }

    //在键盘接受指令,对指令分析运算,输出数据
    while (true)//无数次执行循环体
    {

    ConsoleKeyInfo s = Console.ReadKey();//在键盘接受指令
    int t = map[x,y];

    if (s.Key.ToString() == "RightArrow")//若接受指令为“→”,
    {
    if (map[x, y + 1] == 0)//若右边方格为空格,小人物与空格交换数据
    {
    map[x, y] = map[x, y + 1];
    map[x, y + 1] = t;
    y++;
    }
    else if (map[x, y + 1] == 2 && map[x, y + 2] !=1)//若右边方格为箱子,右边方格接受小人物数据,小人物方格数据变零,右数第二个方格接受箱子方格数据
    {
    int m = map[x, y + 1];
    map[x, y + 1] = t;
    map[x, y] = 0;
    map[x, y + 2] = m;
    y++;

    }
    }
    else if (s.Key.ToString() == "LeftArrow")
    {
    if (map[x, y - 1] == 0)
    {
    map[x, y] = map[x, y - 1];
    map[x, y - 1] = t;
    y--;
    }
    else if (map[x, y - 1] == 2 && map[x, y - 2]!=1)
    {
    int m = map[x, y - 1];
    map[x, y - 1] = t;
    map[x, y] = 0;
    map[x, y - 2] = m;
    y--;

    }
    }
    else if (s.Key.ToString() == "UpArrow")
    {
    if (map[x - 1, y]==0)
    {
    map[x, y] = map[x - 1, y];
    map[x-1,y] = t;
    x--;
    }
    else if (map[x - 1, y] == 2 && map[x-2, y ] != 1)
    {
    int m = map[x-1, y ];
    map[x - 1, y] = t;
    map[x, y] = 0;
    map[x - 2, y] = m;
    x--;

    }


    }
    else if (s.Key.ToString() == "DownArrow")
    {
    if (map[x + 1, y] == 0)
    {
    map[x, y] = map[x + 1, y];
    map[x + 1, y] = t;
    x++;
    }
    else if (map[x + 1, y] == 2 && map[x + 2, y] != 1)
    {
    int m = map[x + 1, y];
    map[x + 1, y] = t;
    map[x , y] = 0;
    map[x + 2, y] =m;
    x++;

    }

    }
    Console.Clear();

    for (int i = 0; i < 10; i++)
    {
    for (int j = 0; j < 10; j++)
    {
    if (map[i, j] == 0)
    {
    Console.Write(" ");
    }
    else if (map[i, j] == 1)
    {
    Console.Write("■");
    }
    else if (map[i, j] == 2)
    {
    Console.Write("□");
    }
    else if (map[i, j] == 3)
    {
    Console.Write("★");
    }
    else if (map[i, j] == 4)
    {
    Console.Write("♀");
    }
    }

    Console.WriteLine();
    }
    if (map[9, 9] == 2)
    Console.WriteLine("恭喜成功!");

    }

  • 相关阅读:
    PHP保留小数的相关方法
    ASP.NET Core MVC 之过滤器(Filter)
    ASP.NET Core MVC 之控制器(Controller)
    ASP.NET Core MVC 之视图组件(View Component)
    ASP.NET Core MVC 之局部视图(Partial Views)
    标签助手(TagHelper)
    ASP.NET Core MVC 之布局(Layout)
    ASP.NET Core MVC 之视图(Views)
    ASP.NET Core MVC 之模型(Model)
    九卷读书:淘宝从小到大的发展 -重读《淘宝技术这十年》
  • 原文地址:https://www.cnblogs.com/wllhq/p/4189874.html
Copyright © 2020-2023  润新知