面试总结
Posted 2186009311cff
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面试总结相关的知识,希望对你有一定的参考价值。
题一:
程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)
要求:
<1>.构造出Cat、Mouse、Master三个类,并能使程序运行
<2>从Mouse和Master中提取抽象
<3>联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒。
using System; using System.Collections; using System.Collections.Generic ; namespace gtkTest { public class ObserverClass { public ObserverClass () { } public static void ObserverFun () { Cat cat = new Cat(); Mouse mouse1 = new Mouse("mouse1", cat); Mouse mouse2 = new Mouse("mouse2", cat); Master master = new Master(cat); cat.Cry(); } 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(); } } } } } //2 using System; namespace gtkTest { public class ObserverDeleClass { public ObserverDeleClass () { } public static void ObserverDeleFun () { Cat cat = new Cat(); Mouse mouse1 = new Mouse("ObserverDelemouse1", cat); Mouse mouse2 = new Mouse("ObserverDelemouse2", 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("ObserverDelecat 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("ObserverDelehost waken"); } } } } //3 gtkTest.ObserverDeleClass.ObserverDeleFun(); gtkTest.ObserverClass.ObserverFun ();
参考: https://blog.csdn.net/hunterxray/article/details/1532674
题二:
一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现
public class MainClass { public static void Main() { Console.WriteLine(Foo(30)); } public static int Foo(int i) { if (i <= 0) return 0; else if(i > 0 && i <= 2) return 1; else return Foo(i -1) + Foo(i - 2); } }
参考:https://blog.csdn.net/zhangzuqing/article/details/20208385
题三
注意:
以上是关于面试总结的主要内容,如果未能解决你的问题,请参考以下文章
2021-12-24:划分字母区间。 字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。 力扣763。某大厂面试