首页 / C# / c#软件工程师笔试题
c#软件工程师笔试题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#软件工程师笔试题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3006字,纯文字阅读大概需要5分钟。
内容图文
近来有打算重新找工作,还没提离职,投了几家公司简历,其中一家比较中意的公司给发了面试题,其实,好像是好几天前的事了,主要是Gmail邮箱很少用,所以一直都没去看,今天看到题目给解了。
题目如下:
题目:
假设我们是中国国家航天局人员,当玉兔号离开嫦娥三号之后,我们需要能够控制玉兔号在月球上开展探测工作。我们先假定虹湾区是一个很大的平原,我们在虹湾区建立一个坐标轴,如下图:
玉兔号离开嫦娥三号后,根据自身安装的定位系统可以知道自己的初始位置,我们记为 X0 , Y0 ; 同时玉兔号也可以知道当前它的朝向,如东、西、南、北(暂时只考虑这四个方向)。
中国国家航天局会向玉兔号发送指令,我们先暂定为3种:
- F : 当玉兔号接收到这条指令之后,会向前移动一个坐标单位的距离
- L : 当玉兔号接受到这条指令之后,会原地向左旋转90度
- R : 当玉兔号接收到这条指令之后,会原地向右旋转90度
要求:
一)设计一个玉兔号的主程序,能够接收中国国家航天局发送过来的指令序列(如FFLFRFLL),执行该指令序列之后,玉兔号能够走到正确的位置,并知道当前正确的位置。(如:玉兔号初始位置为 (0,0),方向朝东,执行指令 FFLFRFLL之后,位置为 (3,1) 方向朝西)
二)主程序中,不允许出现switch case语句,也不允许出现else关键字,也不允许使用三元表达式,if关键字出现的次数要求在5次以下(0-4次)
三)主程序可以用任何语言编写,如Java、C#、Ruby、Python、PHP等
四)在所选语言允许的情况下,请编写相应的单元测试
思路:一般有多条件的,我们会选择使用if/else、switch /case ,但 题目明确规定 不能使用,if也限定次数,很自然就想到委托(c++里面的函数指针),还有怎么实现根据输入自动选择哪种操作,这就想到了字典(键/值).
贴出代码:
public delegate void OperatorDelegate(); static void Main(string[] args) { string instruct = ""; Detector YuTu3 = new Detector(0, 0, (int)Diretion.East); var Dictory = new System.Collections.Generic.Dictionary<string, OperatorDelegate>(); Dictory["F"] = new OperatorDelegate(YuTu3.DealFont); Dictory["L"] = new OperatorDelegate(YuTu3.DealLeft); Dictory["R"] = new OperatorDelegate(YuTu3.DealRight); while ("exit" != (instruct = Console.ReadLine())) { if (Dictory.ContainsKey(instruct)) { Dictory[instruct](); } }; YuTu3.Print(); } //探测器class Detector { delegatevoid DelegateFont(); privateint x; privateint y; privateint direction; privateconstint MaxDirection = 4;//常量表示当前有4个方向private Dictionary<string, DelegateFont> Dictionary = new Dictionary<string, DelegateFont>(); //构造函数public Detector(int x, int y, int direction) { this.x = x; this.y = y; this.direction = direction; Dictionary["0"] = new DelegateFont(NorthAdd); Dictionary["1"] = new DelegateFont(EastAdd); Dictionary["2"] = new DelegateFont(SouthAdd); Dictionary["3"] = new DelegateFont(WestAdd); } ///<summary>/// 逆时针 ///</summary>publicvoid DealLeft() { direction = (direction - 1 + MaxDirection) % MaxDirection; } ///<summary>/// 顺时针 ///</summary>publicvoid DealRight() { direction = (direction + 1) % MaxDirection; } publicvoid DealFont() { //没有使用委托实现 //if (direction == (int)Diretion.North) { ++y; return; } //if (direction == (int)Diretion.South) { --y; return; } //if (direction == (int)Diretion.West) { --x; return; } //++x; //使用委托+字典实现if (Dictionary.ContainsKey(direction.ToString())) { //调用委托 Dictionary[direction.ToString()](); } } publicvoid Print() { Console.WriteLine("x:" + x + ",y:" + y); Console.WriteLine("Direction:" + (Diretion)direction); Console.ReadKey(); } privatevoid NorthAdd() { ++y; } privatevoid SouthAdd() { --y; } privatevoid WestAdd() { --x; } privatevoid EastAdd() { ++x; } } enum Diretion { North = 0, East = 1, South = 2, West = 3 }
使用两个委托+字典替换if/else ,switch/case,这样做的好处就是容易维护和新增数据,当代码需要添加第四种操作,第五种操作的时候,不需要改动调用的方法,只需要在探测器类里面填加操作,键值新增数据即可。
同时也把c#高级的委托跟字典再复习了一遍,收获颇多。不断学习,不断总结,不断成长。
原文:http://www.cnblogs.com/yudeyinji/p/3581008.html
内容总结
以上是互联网集市为您收集整理的c#软件工程师笔试题全部内容,希望文章能够帮你解决c#软件工程师笔试题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。