数组的应用:
(一).冒泡排序。
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. jdk7 中Collections.sort 异常

    Collections.sort 异常 java.lang.IllegalArgumentException: Comparison method violates its general contr ...

  2. C 语言中实现数据与方法的封装

    在 C 语言中可以用结构体代替类,用函数指针代替成员方法,实现数据成员与成员方法的封装,在客户端写出的程序与 C++ 类似,唯一的不同是 C 语言中调用函数指针成员时必须将本对象的地址传给函数,因为 ...

  3. WPF拖动总结[转载]

    WPF拖动总结   这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中 ...

  4. Visual Studio常用的快捷键

      每次在网上搜关于VS有哪些常用快捷键的时候,出来的永远是一串长的不能再长的列表,完全没体现出“常用”二字,每次看完前面几个就看不下去了,相信大家都 有这种感觉.其实我们平时用的真的只有很少的一部分 ...

  5. javascript XMLHttpRequest对象全面剖析

    转载:http://www.jb51.net/article/23175.htm 一. 引言 异步JavaScript与XML(AJAX)是一个专用术语,用于实现在客户端脚本与服务器之间的数据交互过程 ...

  6. 如何写一个数据库How do you build a database?(转载)

    转载自:http://www.reddit.com/r/Database/comments/27u6dy/how_do_you_build_a_database/ciggal8 Its a great ...

  7. 出发 Let's Go

    今天是中秋佳节,而恰好我这天过生日,晚上睡觉前又恰好听到温岚唱的祝我生日快乐,心里挺高兴的. 最近,由于公司需要,可能要学习Python和Tribon了,全是未知的,一点不了解的东西,也忽然想起了在这 ...

  8. <转>golang 并发性能数据

    1.管道chan吞吐极限10,000,000,单次Put,Get耗时大约100ns/op,无论是采用单Go程,还是多Go程并发(并发数:100, 10000, 100000),耗时均没有变化,Go内核 ...

  9. C# 添加服务引用。

    1,服务引用给的实例(需要一个网址连接) http://192.168.17.131:12170/amtiot.gis.WCF/SpatialAnalysis.svc  (类似于这样的一个网址) 在网 ...

  10. 【搜索引擎Jediael开发笔记1】搜索引擎初步介绍及网络爬虫

    详细可参考 (1)书箱:<这就是搜索引擎><自己动手写网络爬虫><解密搜索引擎打桩实践> (2)[搜索引擎基础知识1]搜索引擎的技术架构 (3)[搜索引擎基础知识2 ...