前些时候做过的一份面试试题

本次测试共三道题目,要求如下:

1.  请用C#完成

2.  开卷独立完成,可以看书,上网等任何方式查找资料,但必须保证独立完成。一旦发现有不诚实行为,本公司将不予录取,后果自负。

 

题目一:

1.  编写冒泡排序程序

要求:

1)  请用C#编写一个冒泡排序的程序,

2)  要求排序的数据从文件C:/DATA.DAT中读取,数据间用逗号分隔。

解:

using System;

using System.IO;

class Test{

         static void BubbleSort(int[] ai) {

                   for (int j = ai.Length - 1; j > 0; j--){ // outer loop (backward)

                            for (int i = 0; i < j; i++) // inner loop (forward)

                                      if (ai[i] > ai[i+1])  Swap(ref ai[i], ref ai[i+1]);

                   }

         }

         static void Swap(ref int x,ref int y) {

                   x = x + y;

                   y = x - y;

                   x = x - y;

         }

         static void Main (string[] args) {

                   int[] ai = getData();

                   BubbleSort(ai);

                   foreach (int i in ai) Console.WriteLine(i);

                  Console.ReadLine();

         }

         public static int[] getData(){

                   try{

                      StreamReader sr = new StreamReader("data.dat");

                            string sLine;

                            sLine = sr.ReadLine();

                            string[] astr = sLine.Split(new Char[]{','});

                            int[] ai = new int[astr.Length];                     

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

                                     ai[i] = Convert.ToInt32(astr[i]);

                            return ai;

                   }

                   catch (Exception e)      {

                       // Let the user know what went wrong.

                       Console.WriteLine("The file could not be read:");

                       Console.WriteLine(e.Message);

                       return null;

                   }

         }

}

 

2.完成下面函数

         public static string Left(string sSource, int iLength)

         {

                   //完成类似于VBLeft函数的功能(返回字符串sSource的左iLength位的字符串)。

         }

解:

using System;

 

class Test{

    public static void Main (){

         //完成类似于VBLeft函数的功能(返回字符串sSource的左iLength位的字符串)。

         Console.WriteLine("Please enter a string to treaded:");

         string s = Console.ReadLine();

         int i = 0;

         bool bInputValid = false;

         while(!bInputValid){

                   try{

                            Console.WriteLine("Please enter the length from left most:");

                            i = Convert.ToInt32(Console.ReadLine());

                            if(i > s.Length) throw new Exception("");

                   }

                   catch(Exception e){

                            Console.WriteLine("It seen that your input data is not a integer or over flow, try again!");

                            Console.WriteLine(e.Message);

                            continue;

                   }

                   bInputValid = true;

         }

         Console.WriteLine("The left part string is {0} ", Left(s, i)); 

    }

         public static string Left(string sSource, int iLength){

                   char[] achar = new char[iLength];

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

                            achar[i] = sSource[i];//直接可字串中的单个字符,关键在这儿了

                   string sLeftPart = new string(achar);//直接构造回一个string

                   return sLeftPart;

         } 

}

 

2.   动物(animals)中的猫(cat)和狗(dog)都有咬(bit)的动作。请运用.net中对象的多态性技术展示猫咬和狗咬的动作。要求用C#代码实现。

 

 


 

解:

using System;

 

public class animals{

         //string sName;

         public virtual void bit(){

                   Console.Write("i am animals");

         }

}

 

public class dog : animals{

         public override void bit(){

                   Console.Write("bit by a dag!");

         }

}

 

public class cat : animals{

         public override void bit(){

                   Console.Write("bit by a cat!");

         }

}

 

public class Test{

         public static void Main (){

                   animals[] aa = new animals[2];

                   aa[0] = new dog();

                   aa[1] =  new cat();

                   foreach(animals a in aa) a.bit();

         } 

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值