首页 > 代码库 > 数组 20140823

数组 20140823

一、定义:

数组是用来存储数据连续且相同类型的数据的一块区域,数组中包含的数值称作元素.

二、释义:

                     a[]--下标,指数组内元素的长度,从0开始递增.

                     a[5]--a[0]至a[4]

三、分类:

1.一维数组:a[],数组内包含一组数据,逐个递增,像豆角形式,用于一般运算.

2.二维数组:a[2,1],数组内包含数据由行、列组成,像超市储物柜,格子状组成,用于简单游戏开发(推箱子).

3.三维数组:a[2,1,3]数组内包含数据像立方,像魔方,多用于三维游戏开发(极品飞车).

四、使用:

1.定义:int[] a=new int[5];--数组元素长度是5,最大下标是4;"="右边,"[]"内必须是常量.

2.赋值:a[0]=12,a[1]=14,a[2]=7……

3.取值:int b=a[0],

例:将a[0].a[1]换位.

int b;

b=a[0];

a[0]=a[1];

a[1]=b;

4.运算:如需换位先定义一个新的变量

例1:10个球员,代号为0--9,训练时间裁判给球员打分,输完分数后求总分及平均分;得出最高分及最低分.


            int[] a = new int[10];

            for (int j = 1; j<= 10; j++)
            {
                Console.Write("请输入第{0}个球员的分数:", i+1);

                a[i - 1] = Convert.ToInt32(Console.ReadLine());
            }

            int sum = 0;double avg;
            for (int i = 0; i < 10; i++)
            {
                sum = sum + a[j];

                avg = sum / (10 * 1.0);
            }
            Console.WriteLine("总分是:{0},平均分是:{1}", sum, avg);

            //找最高分、最低分
            int max = a[0];
            int min = a[0];
            int max1 = 0;
            int min1 = 0;

            for (int d = 0; d < 10; d++)
            {
                if ( max<a[d])
                {
                    max = a[d];
                    max1 = d + 1;
                }
                if (min>a[d])
                {
                    min = a[d];
                    min1 = d + 1;
                }
            }
            Console.WriteLine("最高分为:{0}号球员{1}分", "最低分为:{2}号球员{3}分",max1, max,min1, min);
           去掉一个最高分,去掉一个最低分球员最后得分是

            int sum = 0; double avg;                  
            int[] a = new int[10];
            for (int i = 0; i < 10; i++)
            {
                Console.Write("请输入第{0}个球员的分数:", i+1);
                a[i] = Convert.ToInt32(Console.ReadLine());

               
                for (int i = 1; i <10; i++)
                {
                     sum=sum+a[i];
                }
                int max=a[0];int min=a[0];

                for (int i = 1; i <10; i++)
                {
                    if (max < a[i])
                        max = a[i];

                    if (min > a[i])
                        min = a[i];           


                   double avg = (sum - max-min) / (8 * 1.0);
                }               

                Console.WriteLine("去掉一个最高分{0},去掉一个最低分{1},歌手最后得分是{2}", max, min,avg);

例2.5个候选班长,代号0-4,20个人投票,统计得票最多的代号和票数选为班长.

            int[] votes = new int[5];
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("请投票(0-4):");
                int n = Convert.ToInt32(Console.ReadLine());
                if (n >= 0 && n <= 4)
                {
                    votes[n]++;//--投票数为0-4或定义班长姓名为0-4
                }
                else
                {
                    Console.WriteLine("此票为废票.");
                }
            }
                // 找最大值
                int max = votes[0], max1 = 0;
                for (int i = 1; i < 5; i++)
                {
                    if (votes[i] > max)
                    {
                        max = votes[i];
                        max1 = i;
                    }
                }

                Console.WriteLine("{0}以{1}票当选班长!", max1, max);

数组 20140823