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.将一个数 ...
随机推荐
- HMM的学习笔记1:前向算法
HMM的学习笔记 HMM是关于时序的概率模型.描写叙述由一个隐藏的马尔科夫链随机生成不可观測的状态随机序列,再由各个状态生成不可观測的状态随机序列,再由各个状态生成一个观測而产生观測的随机过程. HM ...
- Java中的内存泄漏问题
今天来谈谈Java语言中的内存泄漏问题,可能还有人不知道什么是内存泄漏,先来说下内存泄漏的概念. 内存泄漏:比较正式的说法是,不再使用的对象,却不能被Java垃圾回收机回收.用我的话来说,就是Java ...
- 新一代分布式任务调度框架:当当elastic-job开源项目的10项特性
作者简介: 张亮,当当网架构师.当当技术委员会成员.消息中间件组负责人.对架构设计.分布式.优雅代码等领域兴趣浓厚.目前主导当当应用框架ddframe研发,并负责推广及撰写技术白皮书. 一.为什么 ...
- NET基础课--组件2
强命名组件:使用sn.exe生成公钥私钥对,公钥可以用工具查看.snk文件需严格保护. sn -k d:\iron.snk 生成公钥私钥对 sn -p d:\iron.snk d:\ ...
- P - A + B(第二季水)
Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. ...
- 在vs.net c#中添加mysql模型
http://weblogs.asp.net/gunnarpeipman/getting-mysql-work-with-entity-framework-4-0 http://dev.mysql.c ...
- Servlet 学习总结-2
#重定向与转发的区别 开发Web应用中会遇到从一个页面跳转到另一个页面的问题,在JSP中有两种跳转方式: 1.重定向 2.转发(转向) 重定向:首先服务器受到浏览器客户端请求之后,服务器发送新的链接到 ...
- win7笔记本电脑实现wifi共享
前提条件:win 7系统,有wifi 同dos命令就可实现wifi共享 第一步: netsh wlan start hostednetwork pause 第二步: netsh wlan set ho ...
- php 格式
$abc = ($_POST[' : strtotime($_POST['start_time']); 解析:判断接收的数据是否为0,如果等于0赋值0,若不等于,则赋值获取的数值. strtotime ...
- Eddy's digital Roots(九余数定理)
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...