C#整理6——数组的应用

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

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class2
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[8] { 9, 12, 7, 5, 15, 2, 1, 8 };
13             //冒泡排序。
14             for(int i=1;i<=a.Length-1;i++) //趟数
15             {
16                 for (int j = 1; j <= a.Length - i; j++)//次数
17                 {
18                     if(a[j-1] > a[j])
19                     {
20                         int t = a[j - 1];
21                         a[j - 1] = a[j];
22                         a[j] = t;
23                     }
24                 }
25             }
26 
27             //显示
28             for(int k=0;k<a.Length;k++)
29             {
30                 Console.WriteLine(a[k]);
31             }
32         }
33     }
34 }
View Code


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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class1
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[10];
13             //亮分
14             ShuRu(a);
15 
16             //排序
17             PaiXu(a);
18 
19             //运算求平均
20             double avg = YunSuan(a);
21 
22             //输出显示
23             ShuChu(a, avg);
24         }
25 
26         private static void ShuChu(int[] a, double avg)
27         {
28             Console.WriteLine("去掉两个最高分:" + a[0] + "" + a[1]);
29             Console.WriteLine("去掉两个最低分:" + a[a.Length - 1] + "" + a[a.Length - 2]);
30             Console.WriteLine("该选手最终得分为:" + avg);
31         }
32 
33         private static double YunSuan(int[] a)
34         {
35             //求总分
36             int sum = 0;
37             for (int i = 2; i <= a.Length - 3; i++)
38             {
39                 sum += a[i];
40             }
41             //求平均
42             double avg = (1.0 * sum) / (a.Length - 4);
43             return avg;
44         }
45 
46         private static void PaiXu(int[] a)
47         {
48             for (int i = 1; i <= a.Length - 1; i++)
49             {
50                 for (int j = 1; j <= a.Length - i; j++)
51                 {
52                     if (a[j] > a[j - 1])
53                     {
54                         int temp = a[j];
55                         a[j] = a[j - 1];
56                         a[j - 1] = temp;
57                     }
58                 }
59             }
60         }
61 
62         private static void ShuRu(int[] a)
63         {
64             for (int i = 0; i < a.Length; i++)
65             {
66                 Console.Write("请第" + (i + 1) + "号评委亮分:");
67                 a[i] = Convert.ToInt32(Console.ReadLine());
68             }
69         }
70     }
71 }
View Code

 

代码。

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class3
 9     {
10         static void Main(string[] args)
11         {
12             int[] a = new int[] { 3, 5, 7, 9, 11, 13, 14, 18 };
13 
14             Console.Write("请输入要找的数:");
15             int find = Convert.ToInt32(Console.ReadLine());
16 
17             int top, bottom, mid;  //上限下标,下限下标,中间下标
18             top = 0;
19             bottom = a.Length - 1;
20             
21             while(bottom>=top)  //只要下限下标还在上限下标的下面,就循环,否则没找到就结束。
22             {
23                 //算中间下标
24                 mid = (top + bottom) / 2;
25                 //取中间的值
26                 int n = a[mid];
27                 if(n < find)
28                 {
29                     top = mid + 1;      //调整上限的下标
30                 }
31                 else if(n>find)
32                 {
33                     bottom = mid - 1;// 调整下限的下标。
34                 }
35                 else
36                 {
37                     Console.WriteLine("找到了,在第" + mid + "个元素上");
38                     break;
39                 }
40             }
41         }
42     }
43 }
View Code

 

二维数组:
表格的模型。
定义:
数据类型[,] 数组名 = 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;
取值:
数组名[下标,下标];
应用:

语文 数学 总分

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int[,] a = new int[3, 4];
13             //输入
14             for (int i = 0; i < 3; i++)
15             {
16                 //自动生成学号
17                 a[i, 0] = i + 1;
18                 //语文成绩
19                 Console.Write("语文:");
20                 a[i, 1] = Convert.ToInt32(Console.ReadLine());
21                 //数学成绩
22                 Console.Write("数学:");
23                 a[i, 2] = Convert.ToInt32(Console.ReadLine());
24                 //总分
25                 a[i, 3] = a[i, 1] + a[i, 2];
26 
27             }
28             //显示
29             Console.WriteLine("学号\t语文\t数学\t总分");
30             for (int i = 0; i < 3; i++)
31             {
32                 for (int j = 0; j < 4; j++)
33                 {
34                     Console.Write(a[i, j] + "\t");
35 
36                 }
37                 Console.WriteLine();
38             }
39         }
40     }
41 }
View Code
搬箱子
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace ConsoleApplication1
  7 {
  8     class Program
  9     {
 10         static void Main(string[] args)
 11         {
 12             int x = 3, y = 1;//记录小人初始位置
 13             int[,] map = new int[10, 10]
 14 {
 15 {1,1,1,1,1,1,1,1,1,1},
 16 {1,0,0,0,0,1,0,0,0,1},
 17 {1,0,0,0,0,1,0,0,0,1},
 18 {1,4,2,0,0,0,0,0,0,1},
 19 {1,0,0,0,0,0,0,0,0,1},
 20 {1,0,0,0,0,0,0,0,0,1},
 21 {1,0,0,0,0,0,0,0,0,1},
 22 {1,1,1,1,1,0,0,0,0,1},
 23 {1,0,0,0,0,0,0,0,3,1},
 24 {1,1,1,1,1,1,1,1,1,1}
 25 };
 26 
 27             //在键盘接受指令,对指令分析运算,输出数据
 28             while (true)//无数次执行循环体
 29             {
 30                 for (int i = 0; i < 10; i++)//打印初始图
 31                 {
 32                     for (int j = 0; j < 10; j++)
 33                     {
 34                         if (map[i, j] == 0)
 35                         {
 36                             Console.Write(" ");
 37                         }
 38                         else if (map[i, j] == 1)
 39                         {
 40                             Console.Write("");
 41                         }
 42                         else if (map[i, j] == 2)
 43                         {
 44                             Console.Write("");
 45                         }
 46                         else if (map[i, j] == 3)
 47                         {
 48                             Console.Write("");
 49                         }
 50                         else if (map[i, j] == 4)
 51                         {
 52                             Console.Write("");
 53                         }
 54                     }
 55 
 56                     Console.WriteLine();
 57                 }
 58 
 59                 ConsoleKeyInfo s = Console.ReadKey();//在键盘接受指令
 60                 int t = map[x, y];
 61 
 62                 if (s.Key.ToString() == "RightArrow")//若接受指令为“→”,
 63                 {
 64                     if (map[x, y + 1] == 0)//若右边方格为空格,小人物与空格交换数据
 65                     {
 66                         map[x, y] = map[x, y + 1];
 67                         map[x, y + 1] = t;
 68                         y++;
 69                     }
 70                     else if (map[x, y + 1] == 2 && map[x, y + 2] != 1)//若右边方格为箱子,右边方格接受小人物数据,小人物方
 71 
 72                     //格数据变零,右数第二个方格接受箱子方格数据
 73                     {
 74                         int m = map[x, y + 1];
 75                         map[x, y + 1] = t;
 76                         map[x, y] = 0;
 77                         map[x, y + 2] = m;
 78                         y++;
 79 
 80 
 81                     }
 82                 }
 83                 else if (s.Key.ToString() == "LeftArrow")
 84                 {
 85                     if (map[x, y - 1] == 0)
 86                     {
 87                         map[x, y] = map[x, y - 1];
 88                         map[x, y - 1] = t;
 89                         y--;
 90                     }
 91                     else if (map[x, y - 1] == 2 && map[x, y - 2] != 1)
 92                     {
 93                         int m = map[x, y - 1];
 94                         map[x, y - 1] = t;
 95                         map[x, y] = 0;
 96                         map[x, y - 2] = m;
 97                         y--;
 98 
 99                     }
100                 }
101                 else if (s.Key.ToString() == "UpArrow")
102                 {
103                     if (map[x - 1, y] == 0)
104                     {
105                         map[x, y] = map[x - 1, y];
106                         map[x - 1, y] = t;
107                         x--;
108                     }
109                     else if (map[x - 1, y] == 2 && map[x - 2, y] != 1)
110                     {
111                         int m = map[x - 1, y];
112                         map[x - 1, y] = t;
113                         map[x, y] = 0;
114                         map[x - 2, y] = m;
115                         x--;
116 
117                     }
118 
119 
120                 }
121                 else if (s.Key.ToString() == "DownArrow")
122                 {
123                     if (map[x + 1, y] == 0)
124                     {
125                         map[x, y] = map[x + 1, y];
126                         map[x + 1, y] = t;
127                         x++;
128                     }
129                     else if (map[x + 1, y] == 2 && map[x + 2, y] != 1)
130                     {
131                         int m = map[x + 1, y];
132                         map[x + 1, y] = t;
133                         map[x, y] = 0;
134                         map[x + 2, y] = m;
135                         x++;
136 
137 
138                     }
139 
140                 }
141                 Console.Clear();
142 
143 
144                 if (map[8, 8] == 2)//箱子推到指定位置,跳出循环
145                 {
146                     break;
147                 }
148 
149             }
150             Console.WriteLine("恭喜成功!");
151         }
152     }
153 }
View Code

 











posted on 2015-03-05 16:42  酸甜sky  阅读(428)  评论(0编辑  收藏  举报