首页 > 代码库 > 2014.8.22数组

2014.8.22数组

数组:内存 连续 同一类数据的一块区域

分类:一维数组    二维数组(两个下标)    三维数组

 

一维数组:

1.定义  int[] a=new int[5];//a[0]元素,0下标,长度为5,最大下标是4

2.赋值  a[0] = 12;

3.取值  int b = a[0];

4.运算

 

a.Length  数组a的长度

eg:

 1 //球场上有10个球员,裁判给球员打分(0-9),赋值后,求总分、平均分 2 //把最高分和最低分求出来 3 try 4 { 5     Console.WriteLine("请输入10个球员的成绩:"); 6     int[] players = new int[10];//new是在内存中创建一个数组,把他赋给定义的players数组 7     for (int i = 0; i < players.Length; i++) 8     { 9         //把每次输入的值赋给数组元素10         players[i] = Convert.ToInt32(Console.ReadLine());11     }12     int sum = 0;13     double avg;14     int max = players[0], min = players[0];//从第一个开始比较15     int max_i = 0, min_i = 0;//用于存放球员号码16     for (int i = 0; i < players.Length; i++)17     {18         sum = sum + players[i];//遍历数组所有元素,把每个元素相加19     }20     avg = (double)sum / players.Length;//强制转换为double型21     for (int j = 1; j < players.Length; j++)//从players[1]开始和max min比较22     {23         if (max < players[j])24         {25       max = players[j];//两两比较,把大的值赋给max26       max_i = j;27         }28         if (min > players[j])29         {30       min = players[j];31       min_i = j;32         }33     }34     Console.WriteLine("总分为{0},平均分为{1},", sum, avg);35     Console.WriteLine("最高分为{0},球员号码为{1}", max, max_i + 1);36     Console.WriteLine("最低分为{0},球员号码为{1}", min, min_i + 1);37 }38 catch39 {40     Console.WriteLine("输入有误,请重新启动程序!");41 }
 1 //青歌赛 10个评委给一个歌手打分,去掉一个最高分,去掉一个最低分,求平均得分 2 int[] fen = new int[10]; 3 for (int i = 0; i < fen.Length; i++) 4 { 5     Console.Write("请输入第{0}个评委的打分:", fen[i] + 1); 6     fen[i] = Convert.ToInt32(Console.ReadLine()); 7 } 8 int max = fen[0], min = fen[0];//最高分和最低分分别赋为fen[0] 9 for (int i = 1; i < fen.Length; i++)10 {11     if (max < fen[i])12     {13         max = fen[i];14     }15     if (min > fen[i])16     {17         min = fen[i];18     }19 }20 int sum = 0;21 for (int i = 0; i < fen.Length; i++)22 {23     sum = sum + fen[i];24 }25 double result = (sum - max - min) / (fen.Length - 2) * 1.0;//计算平均分26 Console.WriteLine("选手最后得分为" + result);
 1 //5个班长候选人(0-4),20人投票,统计得票最多的代号和票数选为班长 2 try 3 { 4     int[] names = new int[5]; 5     int num; 6     for (int i = 0; i < 20; i++) 7     { 8         Console.Write("请输入要投给谁一票(0-4):"); 9         num = Convert.ToInt32(Console.ReadLine());10         if (num >= 0 && num <= 4)11         {12       names[num]++;//投给谁的票,累加起来13         }14         else15         {16       Console.WriteLine("废票!");17         }18     }19     int max = names[0];20     int max_i = 0;//存放最大值的下标,即票最多的候选人21     for (int i = 1; i < names.Length; i++)22     {23         if (max < names[i])24         {25       max = names[i];26       max_i = i;27         }28     }29     Console.WriteLine("班长是{0},票数为{1}", max_i, max);30 }31 catch32 {33     Console.WriteLine("输入有误,请重新运行程序!");34 }
 1 //青歌赛,去掉两个最高分,去掉两个最低分,求平均分 2 int[] fen = new int[10]; 3 for (int i = 0; i < fen.Length; i++) 4 { 5     Console.Write("请输入第{0}个评委的打分:", fen[i] + 1); 6     fen[i] = Convert.ToInt32(Console.ReadLine()); 7 } 8 int sum = 0; 9 for (int i = 0; i < fen.Length; i++)10 {11     sum = sum + fen[i];12 }13 int max1 = fen[0], max2 = fen[1];14 int min1 = fen[0], min2 = fen[1];15 for (int i = 1; i < fen.Length; i++)16 {17     if (max1 < fen[i])18     {19         max2 = max1;//max2得分次高20         max1 = fen[i];21     }22     if (min1 > fen[i])23     {24         min2 = min1;25         min1 = fen[i];26     }27 }28 double result = (sum - max1 - max2 - min1 - min2) / (fen.Length - 4) * 1.0;29 Console.WriteLine("选手最后得分为" + result);
 1 //彩票生成器(36选7),不能产生重号 2             int[] nums = new int[7]; 3             Random nnn = new Random(); 4             for (int i = 0; i < nums.Length; i++) 5             { 6                 nums[i] = nnn.Next(36); 7                 for (int j = i; j >= 0; j--) 8                 { 9                     if (j == 0)//第一个随机数直接打印10                     {11                         Console.Write(nums[i] + 1 + " ");//36个随机数包括(0-35),所以要+112                         break;13                     }14                     if (nums[i] == nums[j - 1])//随机数i与前面的数比较,如果相等则跳出循环15                     {16                         i = -1;//因为跳出循环会执行i++语句,所以在此赋予i值为-117                         Console.Clear();//清屏,重新生成随机数18                         break;19                     }20                     while (j == 0)//i与前面每个数都不一样,那么打印这个随机数21                     {22                         Console.Write(nums[i] + 1 + " ");23                         j--;24                     }25                 }26             }

 

2014.8.22数组