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

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class2
{
static void Main(string[] args)
{
int[] a = new int[] { , , , , , , , };
//冒泡排序。
for(int i=;i<=a.Length-;i++) //趟数
{
for (int j = ; j <= a.Length - i; j++)//次数
{
if(a[j-] > a[j])
{
int t = a[j - ];
a[j - ] = a[j];
a[j] = t;
}
}
} //显示
for(int k=;k<a.Length;k++)
{
Console.WriteLine(a[k]);
}
}
}
}

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class1
{
static void Main(string[] args)
{
int[] a = new int[];
//亮分
ShuRu(a); //排序
PaiXu(a); //运算求平均
double avg = YunSuan(a); //输出显示
ShuChu(a, avg);
} private static void ShuChu(int[] a, double avg)
{
Console.WriteLine("去掉两个最高分:" + a[] + "和" + a[]);
Console.WriteLine("去掉两个最低分:" + a[a.Length - ] + "和" + a[a.Length - ]);
Console.WriteLine("该选手最终得分为:" + avg);
} private static double YunSuan(int[] a)
{
//求总分
int sum = ;
for (int i = ; i <= a.Length - ; i++)
{
sum += a[i];
}
//求平均
double avg = (1.0 * sum) / (a.Length - );
return avg;
} private static void PaiXu(int[] a)
{
for (int i = ; i <= a.Length - ; i++)
{
for (int j = ; j <= a.Length - i; j++)
{
if (a[j] > a[j - ])
{
int temp = a[j];
a[j] = a[j - ];
a[j - ] = temp;
}
}
}
} private static void ShuRu(int[] a)
{
for (int i = ; i < a.Length; i++)
{
Console.Write("请第" + (i + ) + "号评委亮分:");
a[i] = Convert.ToInt32(Console.ReadLine());
}
}
}
}

代码。

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Class3
{
static void Main(string[] args)
{
int[] a = new int[] { , , , , , , , }; Console.Write("请输入要找的数:");
int find = Convert.ToInt32(Console.ReadLine()); int top, bottom, mid; //上限下标,下限下标,中间下标
top = ;
bottom = a.Length - ; while(bottom>=top) //只要下限下标还在上限下标的下面,就循环,否则没找到就结束。
{
//算中间下标
mid = (top + bottom) / ;
//取中间的值
int n = a[mid];
if(n < find)
{
top = mid + ; //调整上限的下标
}
else if(n>find)
{
bottom = mid - ;// 调整下限的下标。
}
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;
取值:
数组名[下标,下标];
应用:

语文 数学 总分
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[, ];
//输入
for (int i = ; i < ; i++)
{
//自动生成学号
a[i, ] = i + ;
//语文成绩
Console.Write("语文:");
a[i, ] = Convert.ToInt32(Console.ReadLine());
//数学成绩
Console.Write("数学:");
a[i, ] = Convert.ToInt32(Console.ReadLine());
//总分
a[i, ] = a[i, ] + a[i, ]; }
//显示
Console.WriteLine("学号\t语文\t数学\t总分");
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
Console.Write(a[i, j] + "\t"); }
Console.WriteLine();
}
}
}
}
搬箱子
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = , y = ;//记录小人初始位置
int[,] map = new int[, ]
{
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,}
}; //在键盘接受指令,对指令分析运算,输出数据
while (true)//无数次执行循环体
{
for (int i = ; i < ; i++)//打印初始图
{
for (int j = ; j < ; j++)
{
if (map[i, j] == )
{
Console.Write(" ");
}
else if (map[i, j] == )
{
Console.Write("■");
}
else if (map[i, j] == )
{
Console.Write("□");
}
else if (map[i, j] == )
{
Console.Write("★");
}
else if (map[i, j] == )
{
Console.Write("♀");
}
} Console.WriteLine();
} ConsoleKeyInfo s = Console.ReadKey();//在键盘接受指令
int t = map[x, y]; if (s.Key.ToString() == "RightArrow")//若接受指令为“→”,
{
if (map[x, y + ] == )//若右边方格为空格,小人物与空格交换数据
{
map[x, y] = map[x, y + ];
map[x, y + ] = t;
y++;
}
else if (map[x, y + ] == && map[x, y + ] != )//若右边方格为箱子,右边方格接受小人物数据,小人物方 //格数据变零,右数第二个方格接受箱子方格数据
{
int m = map[x, y + ];
map[x, y + ] = t;
map[x, y] = ;
map[x, y + ] = m;
y++; }
}
else if (s.Key.ToString() == "LeftArrow")
{
if (map[x, y - ] == )
{
map[x, y] = map[x, y - ];
map[x, y - ] = t;
y--;
}
else if (map[x, y - ] == && map[x, y - ] != )
{
int m = map[x, y - ];
map[x, y - ] = t;
map[x, y] = ;
map[x, y - ] = m;
y--; }
}
else if (s.Key.ToString() == "UpArrow")
{
if (map[x - , y] == )
{
map[x, y] = map[x - , y];
map[x - , y] = t;
x--;
}
else if (map[x - , y] == && map[x - , y] != )
{
int m = map[x - , y];
map[x - , y] = t;
map[x, y] = ;
map[x - , y] = m;
x--; } }
else if (s.Key.ToString() == "DownArrow")
{
if (map[x + , y] == )
{
map[x, y] = map[x + , y];
map[x + , y] = t;
x++;
}
else if (map[x + , y] == && map[x + , y] != )
{
int m = map[x + , y];
map[x + , y] = t;
map[x, y] = ;
map[x + , y] = m;
x++; } }
Console.Clear(); if (map[, ] == )//箱子推到指定位置,跳出循环
{
break;
} }
Console.WriteLine("恭喜成功!");
}
}
}
												

C#整理6——数组的应用的更多相关文章

  1. php整理(二): 数组

    数组: 首先说一下对PHP中的理解,建立一个好的理解模型还是很关键的: 1.PHP中的数组实际上可以理解为键值对,key=>value;而对于key的取值,可以是string/integer;v ...

  2. 个人整理的数组splay板子,指针的写的太丑了就不放了。。

    splay的板子.. 由于被LCT榨干了..所以昨天去学了数组版的splay,现在整理一下板子.. 以BZOJ3224和3223为例题..暂时只有这些,序列的话等有时间把维修序列给弄上来!! BZOJ ...

  3. 8、C#基础整理(数组和冒泡排序)

    数组 概念:定义一组同类型的指定个数的变量,索引从0开始 例: ];//定义一组有10个数据的数组 shuname[] = ; Console.WriteLine(shuname[]);//打印出1 ...

  4. javascript笔记整理(数组对象)

    1.属性 a.length--设置或返回数组元素的数目 var a=[1,2,3,45,5]; alert(a.length=6) 结果:6 alert(a[5]) 结果:undefined b.co ...

  5. javascript笔记整理(数组)

    数组是一个可以存储一组或是一系列相关数据的容器. 一.为什么要使用数组. a.为了解决大量相关数据的存储和使用的问题. b.模拟真是的世界. 二.如何创建数组 A.通过对象的方式来创建——var a= ...

  6. 【IT笔试面试题整理】数组中出现次数超过一半的数字

    [试题描述]数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. [试题分析]时间复杂度O(n),空间复杂度O(1) 思路1: 创建一个hash_map,key为数组中的数,value为此数 ...

  7. 【leetcode题目整理】数组中找子集

    368. Largest Divisible Subset 题意:找到所有元素都不同的数组中满足以下规则的最大子集,规则为:子集中的任意两个元素a和b,满足a%b=0或者b%a=0. 解答:利用动态规 ...

  8. 【python 数据结构】相同某个字段值的所有数据(整理成数组包字典的形式)

    class MonitoredKeywordMore(APIView): def post(self, request): try: # 设置原生命令并且请求数据 parents_asin = str ...

  9. java:编程比赛中有用的方法整理(一)数组

    我曾经参加过几次编程比赛,但是当时用的是c语言,现在学习了java,打算专攻java组,故以此整理. 数组无论在哪里都必不可少. 一.数组的拷贝: 使用Arrays类的copyOf方法: 1.将一个数 ...

随机推荐

  1. android-读取Assets图片资源保存到SD - 随心

    public class ReadBitmap { public void readByte(Context c, String name, int indexInt) { byte[] b = nu ...

  2. vs在线工具杂烩

    http://visualstudiogallery.msdn.microsoft.com/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Va ...

  3. win8下 web测试 之 hosts绑定

    从这个开始,开启web测试之旅 绑定hosts: 1.在C:\Windows\System32\drivers\etc下找到 hosts 文件 2.将hosts文件复制到一个地方: 3.修改hosts ...

  4. iOS 性能优化:Instruments

    对于每位 iOS 开发者来说,代码性能是个避不开的话题.随着项目的扩大和功能的增多,没经过认真调试和优化的代码,要么任性地卡顿运行,要么低调地崩溃了之……结果呢,大家用着不高兴,开发者也不开心. 其实 ...

  5. 关于box-sizing的理解

    ---恢复内容开始--- box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. 例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "bord ...

  6. oracle使用口令文件验证和os验证

    一.Oracle安装之后默认情况下是启用了OS认证的,这里提到的os认证是指服务器端os认证.OS认证的意思把登录数据库的用户和口令校验放在了操作系统一级.如果以安装Oracle时的用户登录OS,那么 ...

  7. android sdk manager无法更新

    问题描述:       Android SDK Manager 无法下载更新,或者更新速度超慢,或者待安装包列表不显示.   解决方法:     第一,我们先修改下hosts文件.该文件的位置在系统盘 ...

  8. Linux下,命令 wget 的使用

    wget是一个从网络上自动下载文件的自由工具.它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理.  所谓的自动下载是指,wget可以在用户退出系统的之后在后台执行.这意味这你可以登录系统, ...

  9. 简单C# 验证类

    using System; using System.Text.RegularExpressions; namespace bobomousecom.crm { /// <summary> ...

  10. python基础教程第5章——条件循环和其他语句

    1.语句块是在条件为真(条件语句)时执行或者执行多次(循环语句)的一组语句.在代码前放置空格来缩进语句即可穿件语句块.块中的每行都应该缩进同样的量.在Phyton中冒号(:)用来标识语句块的开始,块中 ...