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.将一个数 ...
随机推荐
- Hibernate绑定参数
使用绑定参数的优势: 我们为什么要使用绑定命名参数?任何一个事物的存在都是有其价值的,具体到绑定参数对于HQL查询来说,主要有以下两个主要优势:①. 可以利用数据库实施性能优化 因为对Hibernat ...
- Oracle学习笔记(2)——过程和函数
过程和函数统称为PL/SQL子程序,通过输入.输出参数或输入/输出参数与其调用者交换信息.他们是被命名的PL/SQL块,被编译后存储在数据库中,以备执行.因此,可以在数据库中直接按名称使用它们. 1. ...
- JavaScript 运行机制详解:深入理解Event Loop
Philip Roberts的演讲<Help, I'm stuck in an event-loop>,详细.完整.正确地描述JavaScript引擎的内部运行机制. 一.为什么JavaS ...
- svn 清理失败 (cleanup 失败) 的解决方法
svn 清理失败 (clean up 失败) 的解决方法 參考:http://www.tuicool.com/articles/biy6na 解决方法: step1: 到 sqlite官网 (http ...
- Android 4.4 Kitkat Phone工作流程浅析(六)__InCallActivity显示更新流程
本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处 本文代码以MTK平台Android 4.4为分析对象,与Google原生AOSP有些许差异,请读者知悉. ...
- ORACLE数据库不同故障下的恢复总结
ORACLE数据库不同故障下的恢复总结1. 非归档模式下丢失或损坏的文件--1.1 数据文件--启动数据库的状态到MOUNT--恢复方法:通过之前创建的数据库完整备份,修复整个数据库,不过备份之后发生 ...
- iOS 性能优化:Instruments
对于每位 iOS 开发者来说,代码性能是个避不开的话题.随着项目的扩大和功能的增多,没经过认真调试和优化的代码,要么任性地卡顿运行,要么低调地崩溃了之……结果呢,大家用着不高兴,开发者也不开心. 其实 ...
- 在sql server中怎样获得正在执行的Sql查询
方法1:使用DBCC inputbuffer(spid) 使用SP_WHO获得SPID,然后再执行上面的DBCC command,参见下图 执行一段sql语句 打开另一个query窗口并执行SP_WH ...
- Java并发编程之闭锁简介
闭锁相当于一扇门,在闭锁到达结束状态之前,这扇门一直是关闭着的,没有任何线程可以通过,当到达结束状态时,这扇门才会打开并容许所有线程通过.它可以使一个或多个线程等待一组事件发生.闭锁状态包括一个计数器 ...
- [zz]npm安装错误解决方法
错误: npm ERR! at Object.parse (native) npm ERR! at Packer.readRules (/usr/local/lib/node_modules/npm/ ...