C#整理6——数组的应用
数组的应用:
(一).冒泡排序。
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——数组的应用的更多相关文章
- php整理(二): 数组
数组: 首先说一下对PHP中的理解,建立一个好的理解模型还是很关键的: 1.PHP中的数组实际上可以理解为键值对,key=>value;而对于key的取值,可以是string/integer;v ...
- 个人整理的数组splay板子,指针的写的太丑了就不放了。。
splay的板子.. 由于被LCT榨干了..所以昨天去学了数组版的splay,现在整理一下板子.. 以BZOJ3224和3223为例题..暂时只有这些,序列的话等有时间把维修序列给弄上来!! BZOJ ...
- 8、C#基础整理(数组和冒泡排序)
数组 概念:定义一组同类型的指定个数的变量,索引从0开始 例: ];//定义一组有10个数据的数组 shuname[] = ; Console.WriteLine(shuname[]);//打印出1 ...
- javascript笔记整理(数组对象)
1.属性 a.length--设置或返回数组元素的数目 var a=[1,2,3,45,5]; alert(a.length=6) 结果:6 alert(a[5]) 结果:undefined b.co ...
- javascript笔记整理(数组)
数组是一个可以存储一组或是一系列相关数据的容器. 一.为什么要使用数组. a.为了解决大量相关数据的存储和使用的问题. b.模拟真是的世界. 二.如何创建数组 A.通过对象的方式来创建——var a= ...
- 【IT笔试面试题整理】数组中出现次数超过一半的数字
[试题描述]数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. [试题分析]时间复杂度O(n),空间复杂度O(1) 思路1: 创建一个hash_map,key为数组中的数,value为此数 ...
- 【leetcode题目整理】数组中找子集
368. Largest Divisible Subset 题意:找到所有元素都不同的数组中满足以下规则的最大子集,规则为:子集中的任意两个元素a和b,满足a%b=0或者b%a=0. 解答:利用动态规 ...
- 【python 数据结构】相同某个字段值的所有数据(整理成数组包字典的形式)
class MonitoredKeywordMore(APIView): def post(self, request): try: # 设置原生命令并且请求数据 parents_asin = str ...
- java:编程比赛中有用的方法整理(一)数组
我曾经参加过几次编程比赛,但是当时用的是c语言,现在学习了java,打算专攻java组,故以此整理. 数组无论在哪里都必不可少. 一.数组的拷贝: 使用Arrays类的copyOf方法: 1.将一个数 ...
随机推荐
- 认识Junit
JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个. JUnit有它自己的JUn ...
- Hibernate更新部分字段浅谈
update语句是在Hibernate的Configuration的时候生成的,不能动态改变.为什么update的时候所有的属性都一起update,而不是只更新改变字段,其实这是一个比较值得探讨的问题 ...
- MHA自动切换流程
MHA的全名叫做mysql-master-ha,配置后可以在10-30秒内完成master自动切换,切换过程如下: 1. 检测master的状态,方法是一秒一次“ SELECT 1 As Value” ...
- zabbix安装及配置(rpm包安装mysql,php,apache,zabbix)
zabbix安装及配置 一.安装mysql.php.apache.zabbix 安装环境: 操作系统:rhel6.3-x86-64 mysql:5.6.23 --官网下载rpm包安装php:5. ...
- Xshell同时向多个会话发送指令的方法
我们平时使用XSHELL.SecureCRT.putty等ssh连接工具连接到远程主机,每次输入指令都是在单一会话窗口,如果有很多台会话,需要同时输入同样的指令,我们就不用一一输入,浪费时间和精力.可 ...
- Android多项目依赖在Eclipse中无法关联源代码的问题解决 Ctril 点不进去的解决方法
1. 使用快捷键:Ctrl+shift+R,在弹出框中输入.classpath 找到被作为library引入的那个.classpath文件. 2.将kind="src" path ...
- differ比较两个字符串的差异
"abcde","abdefk" ---->-c,+f,+k "aba","aababb" -----&g ...
- NULL & nil & Nil & NSNULL的区别
nil 是 OC 的,空对象,地址指向 空(0) 的对象 在 OC 中,可以给空对象发送任何消息,不会出现错误 NULL 是 C 的,空地址,地址的数值是 0,是一个长整数 表示地址是空 NSNull ...
- c++对文件操作的支持(一)
#include <stdio.h> #include <iostream> #include <fstream> using namespace std; voi ...
- Can't create a new thread (errno 11); if you are not out of available memory, you can consult the manual for a possible OS-dependent bug
解决方案: http://www.javatang.com/archives/2013/06/19/2701909.html