博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面试题(C#算法编程题)
阅读量:5070 次
发布时间:2019-06-12

本文共 6149 字,大约阅读时间需要 20 分钟。

1>用C#写一段选择排序算法,要求用自己的编程风格。

答:private int min;
    public void xuanZhe(int[] list)//选择排序
    {
        for (int i = 0; i < list.Length - 1; i++)
        {
            min = i;
            for (int j = i + 1; j < list.Length; j++)
            {
                if (list[j] < list[min])
                    min = j;
            }
            int t = list[min];
            list[min] = list[i];
            list[i] = t;
        }
    }

 

首先是二分查找法,时间复杂度O(2log2(n)):

        static bool Find(int[] sortedArray, int number)
        {
            if (sortedArray.Length == 0)
                return false;
            int start = 0;
            int end = sortedArray.Length - 1;
            while (end >= start)
            {
                int middle = (start + end) / 2;
                if (sortedArray[middle] < number)
                    start = middle + 1;
                else if (sortedArray[middle] > number)
                    end = middle - 1;
                else
                    return true;
            }
            return false;
        }
然后是三分查找算法,时间复杂度O(3log3(n)):
       static bool Find(int[] sortedArray, int number)
        {
            if (sortedArray.Length == 0)
                return false;
            int start = 0;
            int end = sortedArray.Length - 1;
            while (end >= start)
            {
                int firstMiddle = (end - start) / 3 + start;
                int secondMiddle = end - (end - start) / 3;
                if (sortedArray[firstMiddle] > number)
                    end = firstMiddle - 1;
                else if (sortedArray[secondMiddle] < number)
                    start = secondMiddle + 1;
                else if (sortedArray[firstMiddle] != number && sortedArray[secondMiddle] != number)
                {
                    end = secondMiddle - 1;
                    start = firstMiddle + 1;
                }
                else
                    return true;
            }
            return false;
        }
 
2>一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
private static int Digui(int j)
{
if (j<=0)
return 0;
if (j == 1)
return 1;
return Digui(j-1) + Digui(j - 2);
}
1+2………………n
private static int Digui1(int j)
{
if (j == 0)
return 0;
return Digui1(j - 1) + j;
}

写一个函数计算当参数为N的值:1-2+3-4+5-6+7……+N

答:public int returnSum(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
        {
            int k = i;
            if (i % 2 == 0)
            {
                k = -k;
            }
            sum = sum + k;
        }
        return sum;
        
    }
    public int returnSum1(int n)
    {
        int k = n;
        if (n == 0)
        {
            return 0;
        }
        if (n % 2 == 0)
        {
            k = -k;
        }
        return aaa(n - 1) + k;
    }

 

3>20个随机数列

Dictionary<string, int[]> dict = new Dictionary<string, int[]>();
            ArrayList newArray;
            int[] intArray;
            Random rd;
            while (dict.Count < 20)
            {
                intArray = new int[5];
                rd = new Random();
                newArray = new ArrayList();
                while (newArray.Count < 5)
                {
                    int tempNumber = rd.Next(0, 10);
                    if (!newArray.Contains(tempNumber))
                        newArray.Add(tempNumber);
                }
                string str=ArrayConvertToString(newArray);
                for (int i = 0; i < 5; i++)
                {
                    intArray[i] = (int)newArray[i];
                }
                if (!dict.ContainsKey(str))
                {
                    dict.Add(str, intArray);
                }
            }
            
            foreach (var l in dict)
            {
                Console.WriteLine(l.Key);
            }
            Console.WriteLine("@@" + dict.Keys.Max() + "@@" + dict.Keys.Min());
            Console.ReadKey();
        }
        static public  string ArrayConvertToString(ArrayList al)
        {
            string strTemp=string.Empty;
            foreach (var aList in al)
            {
                strTemp += aList;
            }
            return strTemp;
        }

4>

public abstract class A

    {
        public A()
        {
            Console.WriteLine('A');
        }
        public virtual void Fun()
        {
            Console.WriteLine("A.Fun()");
        }
    }
    public class B : A
    {
        public B()
        {
            Console.WriteLine('B');
        }
        public new void Fun()
        {
            Console.WriteLine(" B.Fun()");
        }
        public static void Main()
        {
            A a = new B();
            a.Fun();
            Console.Read();
        }
 A B  A.fun()

public class A

    {
        public virtual void Fun1(int i)
        {
            Console.WriteLine(i);
        }
        public void Fun2(A a)
        {
            a.Fun1(1);
            Fun1(5);
        }
    }
    public class B : A
    {
        public override void Fun1(int i)
        {
            base.Fun1(i + 1);
        }
        public static void Main()
        {
            A a = new A();
            B b = new B();
            a.Fun2(b);
            b.Fun2(a);
            Console.Read();
        }
    }

2 5 1 6

6. 写出程序的输出结果
class Class1  {
       private string str = "Class1.str";
       private int i = 0;2
       static void StringConvert(string str)  {
           str = "string being converted.";
       }
       static void StringConvert(Class1 c)  {
           c.str = "string being converted.";
       }
       static void Add(int i)  {
           i++;
       }
       static void AddWithRef(ref int i)  {
           i++;
       }
       static void Main()  {
           int i1 = 10;
           int i2 = 20;
           string str = "str";
           Class1 c = new Class1();
           Add(i1);
           AddWithRef(ref i2);21
           Add(c.i);
           StringConvert(str);         
           StringConvert(c);
           Console.WriteLine(i1);
           Console.WriteLine(i2);
           Console.WriteLine(c.i);
           Console.WriteLine(str);
           Console.WriteLine(c.str);
       }  
    }
答:10,21,0,str,string being converted.
10.  程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)
要求:  1.要有联动性,老鼠和主人的行为是被动的。
2.考虑可扩展性,猫的叫声可能引起其他联动效应。
 public interface Observer
    {
        void Response();    //观察者的响应,如是老鼠见到猫的反映
    }
    public interface Subject
    {
        void AimAt(Observer obs);  //针对哪些观察者,这里指猫的要扑捉的对象---老鼠
    }
    public class Mouse : Observer
    {
        private string name;
        public Mouse(string name, Subject subj)
        {           
            this.name = name;
            subj.AimAt(this);
        }
        
        public void Response()
        {
            Console.WriteLine(name + " attempt to escape!");
        }
    }
    public class Master : Observer
    {   
        public Master(Subject subj)
        {           
            subj.AimAt(this);
        }
        
        public void Response()
        {
            Console.WriteLine("Host waken!");
        }  
    }
 
    public class Cat : Subject
    {
        private ArrayList observers;
        public Cat()
        {   
            this.observers = new ArrayList();
        }
        public void AimAt(Observer obs)
        {
            this.observers.Add(obs);
        }
        public void Cry()
        {
            Console.WriteLine("Cat cryed!");
            foreach (Observer obs in this.observers)
            {
                obs.Response();
            }
        }
    }
    class MainClass
    {       
        static void Main(string[] args)
        {
            Cat cat = new Cat();
            Mouse mouse1 = new Mouse("mouse1", cat);
            Mouse mouse2 = new Mouse("mouse2", cat);
            Master master = new Master(cat);
            cat.Cry();
        }
    }
//---------------------------------------------------------------------------------------------
设计方法二: 使用event -- delegate设计..
    public delegate void SubEventHandler();
    public abstract class Subject
    {
        public event SubEventHandler SubEvent;
        protected void FireAway()
        {
            if (this.SubEvent != null)
                this.SubEvent();
        }   
    }
    public class Cat : Subject
    {  
        public void Cry()
        {
            Console.WriteLine("cat cryed.");
            this.FireAway();
        }
    }
    public abstract class Observer
    {
        public Observer(Subject sub)
        {
            sub.SubEvent += new SubEventHandler(Response);
        }
        public abstract void Response();    
    }
    public class Mouse : Observer
    {
        private string name;
        public Mouse(string name, Subject sub) : base(sub)
        {   
            this.name = name;
        }
        public override void Response()
        {
            Console.WriteLine(name + " attempt to escape!");
        }
    }
    public class Master : Observer
    {
        public Master(Subject sub) : base(sub){}
        public override void Response()
        {
            Console.WriteLine("host waken");
        }
    }
    class Class1
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat();
            Mouse mouse1 = new Mouse("mouse1", cat);
            Mouse mouse2 = new Mouse("mouse2", cat);
            Master master = new Master(cat);
            cat.Cry();
        }
    }

转载于:https://www.cnblogs.com/shenqiboy/p/3551528.html

你可能感兴趣的文章
Delphi- 内置数据库的使用例子BDE
查看>>
vj1011:记忆化搜索
查看>>
Json格式的字符串转换为正常显示的日期格式
查看>>
[转]使用 Razor 进行递归操作
查看>>
[转]Android xxx is not translated in yyy, zzz 的解决方法
查看>>
docker入门
查看>>
Android系统--输入系统(十一)Reader线程_简单处理
查看>>
监督学习模型分类 生成模型vs判别模型 概率模型vs非概率模型 参数模型vs非参数模型...
查看>>
Mobiscroll脚本破解,去除Trial和注册时间限制【转】
查看>>
实验五 Java网络编程及安全
查看>>
32位与64位 兼容编程
查看>>
[数据库]关于三个比较典型的数据库试题(1.找到员工表中工资最高的前三名;2.找到员工表中薪水大于本部门平均薪水的员工;3.统计每年入职的员工个数)...
查看>>
iOS-数据解析XML解析的多种平台介绍
查看>>
Linux添加FTP用户并设置权限
查看>>
java 开发面试题小整理(二)
查看>>
创业者靠什么成功?
查看>>
codevs 1222 信与信封问题
查看>>
SDU暑假排位第一场 (Gym - 100889)
查看>>
因数组越界破坏了栈而导致的错误
查看>>
用Chrome devTools 调试Android手机app中的web页面。
查看>>