• 看到的三道测试题


    在MSDN上看到了三道测试题,感觉挺好,帖过来看看:
    本次测试共三道题目,要求如下:

    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)

             {

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

             }

    解:

    using System;

    class Test{

        public static void Main(){

             //完成类似于VB的Left函数的功能(返回字符串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();

             } 

    }

  • 相关阅读:
    中心极限定理
    BCEloss和交叉熵损失的区别
    postgresql的python接口
    DataGrip 2020.1 安装与激活方法
    区块链技术
    TensorRT推理加速推断并提高吞吐量
    纯视觉取代slam做路径规划及避障的思路
    DL重新回顾总结
    OpenCV 轮廓方向
    OpenCV 低通滤波(可设置频率)
  • 原文地址:https://www.cnblogs.com/Spring/p/115096.html
Copyright © 2020-2023  润新知