数组和集合

一.选择题

1.在C#中定义一个数组,以下正确的是【    】。

A) int a=new int[5];                                         B) int[] a= new int[5];                       

C) int a=new int[5];                                           D) int[5] a=new int[];   

2.下列语句中,不能正确定义长度为4的数组a的语句是【    】。

A) int[] a=new int[]{1,2,3,4};                             B) int[] a= {1,2,3,4};                         

C) int[] a=new int[4]{1,2,3};                             D) int[] a=new int[4]{1,2,3,4};    

3.假定int类型变量占用4个字节,若有定义:

int[] x=new int[10]{0,2,4,4,5,6,7,8,9,10};

则数组x在内存中所占字节数是【    】。

A) 10                           B) 20                           C) 40                           D) 80

4.以下数组定义语句中不正确的是【    】。

A) int[] a=new int[5]{1,2,3,4,5};                       B) int[,] a=new int[3][4] ;                   

C) int[][] a=new int[3][0];                        D) int[] a={1,2,3,4,5};

5.以下定义并初始化一维数组的语句中正确的是【    】。

A) int a[] = {1,2,3,4,5};                                    B) int[] a=new int[];

C) int[] a=new int[]{1,2,3,4,5};                         D) int[] a;  a={1,2,3,4,5};

6.以下定义并初始化一维数组的语句中正确的是【    】。

A) int a= new int[];                                           B) int[] a=new int[];

C) int[] a=new int[]{1,2,3,4,5};                         D) int[] a=new int[n]{1,2,3,4,5};

7.以下定义并初始化数组的语句中正确的是【    】。

A) int a[][]=new int[4,5];                                  B) int[][] a=new int[4,5];     

C) int a[,]=new int[4,5];                        D) int[,] a=new int[4,5];

8.有定义语句:int[,] a=new int[5,6];  则下列正确的数组元素引用是【    】。

A) a(3,4)                     B) a(3)(4)                     C) a[3][4]                     D) a[3,4]

9.对于foreach语句和for语句,下列说法错误的是【    】。

A) for语句与foreach语句在结构上不一样,前者有3个表达式,表达式间用分号隔开;后者仅有一个“表达式”,形式为“数据类型 变量 in 集合表达式”

B) 语句for(;true;);是合法的,但是个死循环;“foreach(true);”也是合法的,也是一个死循环

C) 语句for(;true;);是合法的,但是个死循环;而“foreach(true);”是非法的

D) 语句块“ int[] a={1,2}; foreach(int i in a);”是合法的。

10.在C#中,关于Array和ArrayList的维数,以下说法正确的是【    】。

A) Array可以有多维,而ArrayList只能是一维

B) Array只能是一维的,而ArrayList可以有多维

C) Array和ArrayList都只能是一维

D) Array和ArrayList都可以是多维

11.以下List<T>集合声明中正确的是【    】。

A) List<int> f=new List<int>();                        B) List<int> f=new List ();                 

C) List f=new List ();                                      D) List<int> f=new List<int>;

二.填空题

1.数组定义与赋初值语句如下:int[] a={1,2,3,4,5}; 则a[2]的值为【  3  】。

2.下列程序段执行后,a[4]的值为【 4   】。

             int[] a={1,2,3,4,5};

             a[4]=a[a[2]];

3.下列数组定义语句中:int[] a=new int[3]; ,定义的数组a占的字节数为【 12   】。

4.下列数组定义语句中,数组将在内存中占用【 160   】个字节。

             double[,] d=new double [4,5];

5.要定义一个3行4列的单精度二维数组f,使用的定义语句为【  float[,] f=new float [3,4];  】。

6.要定义一个int型的参差数组a,数组有两行,第一行一个元素,第二行两个元素并赋初值3和4,使用的语句如下,请填空。

             int[][] a=【new int[2][]  】;

             a[0]=【 new int[1]    】;

             a[1]=【 new int[2]{3,4} 】;

7. 下列程序完成的功能是采用二分查找方法在给定的有序数组a中查找用户输入的值,并提示相应的查找结果。请填空。

              public static void Main(string[] args)

              {     double [] a=new int[10]{0, 1.2, 2.5, 3, 4.6, 5.0, 6.7, 7.6, 8.2, 9};

                double k; 

                int low=0, high=9, mid;

                Console.Write("输入查找数据:");

                k=double.Parse(Console.ReadLine());           

while (【(1)】)                                          

                 {   mid=(low+high)/2;

                     if (a[mid] == k)

                    {    Console.WriteLine("a[{0}]={1}", mid, k);

                        return;    

                    }

                    else if (a[mid] > k)  【(2)】;               

                    else  low = mid + 1;

                }

                Console.WriteLine("未找到{0}", k);

              }

       (1)low<=high (2)high =mid-1

三.读程序题

1.下列程序的输出结果是【852】。

              class Program

              {     public static void Main(string[] args)

                     {     int  i;

                            int[] a=new int[10];

                            for(i=9;i>=0;i--)  a[i]=10-i;

                            Console.WriteLine(“{0}{1}{2}”,a[2],a[5],a[8]);

                     }

              }

2.下面程序的运行结果是【    】。

              class Program

              {     public static void Main(string[] args)

               {     const int SIZE=10;

int[] y={1,2,3,4,5,6,7,8,9,10};

int[] z=new int[SIZE];

for (int i = 0; i < z.Length; i++)

{     z[i] = i * 2 + y[i];

                                Console.Write("{0} ", z[i]);

}                         

                            Console.Read();

              }

1 4 7 10 13 16 19 22 25 28

2.下面程序的运行结果是【    】。

static void Main(string[] args)

{   const int M = 5;                   

    int[][] yhsj = new int[M][];    

    int i, j;

    for (i = 0; i < M; i++)  yhsj[i] = new int[i + 1];

    for (i = 0; i < M; i++)     { yhsj[i][0] = 1;  yhsj[i][i] = 1;   }

    for (i = 2; i < M; i++)

         for (j = 1; j < i; j++)

                     yhsj[i][j] = yhsj[i - 1][j - 1] + yhsj[i - 1][j];

    for (i = 0; i < M; i++)

    {     for (j = 0; j <= i; j++)  Console.Write("{0}   ", yhsj[i][j]);

              Console.WriteLine();

     }

     Console.Read();

}

1

1   1

1   2   1

1   3   3   1

1   4   6   4   1

3.下面程序的运行结果是【 s=28  】。

static void Main(string[] args)

        {   int s = 0;

            int[][] a = new int[2][];

            a[0] = new int[3] { 1, 2, 3 };

            a[1] = new int[4] { 4, 5, 6, 7 };

            for (int i = 0; i < a.Length; i++)

                   for (int j = 0; j < a[i].Length; j++)

                    s = s + a[i][j];

            Console.Write("s={0}",s);

              }

3.下面程序的运行结果是【 3  】。

      static void Main(string[] args)

     {    int[] num = { 1, 3, 5 };

         ArrayList arr = new ArrayList();

         for(int i=0;i<num.Length;i++)  arr.Add(num[i]);

         arr.Insert(1, 2);

         Console.WriteLine("{0}",arr[2]);       

}  

4.下面程序的运行结果是【 9,7,5,3,1  】。

static void Main(string[] args)

 {  int[] num = { 1,3,5,7,9 };

         Array.Reverse(num);

     foreach (int m in num) Console.Write("{0} ",m);

          Console.Read();                  

       }

5.下面程序的运行结果是【   】。

static void Main(string[] args)

{  ArrayList myarr = new ArrayList();

    myarr.Add("Visual");   myarr.Add("C#");

    myarr.Add("2008");    myarr.Insert(0,"Welcome");       

myarr.Insert(1, "to");         myarr.Remove("2008");     

    foreach (String s in myarr)  Console.Write(s+" ");

    Console.WriteLine();

}

Welcome to Visual C#

编程题

1.设计一个控制台应用程序,假设10个整数用一个一维数组存放,求其最大值和次大值。

static void Main(string[] args)

        {   int[] a = new int[10]{1,8,3,4,7,9,6,10,2,5};

            int n=10,max1,max2,i;

            max1=a[0]>a[1]?a[0]:a[1];     max2=a[0]>a[1]?a[1]:a[0];

            for (i=2;i<n;i++)

                if (max1<a[i])  {   max2=max1;   max1=a[i];  }

            Console.WriteLine("max1={0},max2={1}",max1,max2);

        }

2.设计一个控制台应用程序,用两个二维数组分别存放5个学生4门功课的考试成绩,求每位考生的平均成绩,并输出结果。

static void Main(string[] args)

{  const int Max = 5;                                    //考生数

   int[] Ave = new int[Max];                               //定义一个一维数组存储考生的总成绩

   int[,] grade={{88,75,62,84},{96,85,75,92}, {68,63,72,78},

{95,89,76,98}, {76,65,72,63}};

//定义二维数组存储考生成绩

   for(int i=0; i<Max; i++)

            for(int j=0; j<4; j++)     Ave[i] += grade[i,j];  

   for (int k = 0; k < Max; k++)

      Console.WriteLine("考生{0}平均成绩={1} ",k+1, Ave[k]/4.0);

}

3.设计一个控制台应用程序,用两个一维数组分别存放5个学生的学号和姓名,分别按学号和姓名进行排序,并输出排序后的结果。

class Program

    {   const int Max = 5;

        static void disp(int[] no,string[] name, string str)

        {   Console.WriteLine(str);

            Console.Write("学号:\t");

            for (int i = 0; i < no.Length; i++) 

Console.Write("{0}\t",no[i]);

            Console.WriteLine();

            Console.Write("姓名:\t");

            for (int i = 0; i < name.Length; i++)    

Console.Write("{0}\t", name[i]);

            Console.WriteLine();

        }

        static void Main(string[] args)

        {   int[] no = new int[] { 2, 4, 5, 1, 3};

            string[] name = new string[]

{"Smith","John","Mary","Cherr","Tomn"};

            disp(no, name,"排序前:");

            Array.Sort(no, name);

            disp(no, name,"按学号排序后:");

            Array.Sort(name, no);

            disp(no, name, "按姓名排序后:");

Console.Read();

        }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值