面试题(C#算法编程题)
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();
}
}
面试题(C#算法编程题)的更多相关文章
- C算法编程题系列
我的编程开始(C) C算法编程题(一)扑克牌发牌 C算法编程题(二)正螺旋 C算法编程题(三)画表格 C算法编程题(四)上三角 C算法编程题(五)“E”的变换 C算法编程题(六)串的处理 C算法编程题 ...
- C算法编程题(七)购物
前言 上一篇<C算法编程题(六)串的处理> 有些朋友看过我写的这个算法编程题系列,都说你写的不是什么算法,也不是什么C++,大家也给我提出用一些C++特性去实现问题更方便些,在这里谢谢大家 ...
- C算法编程题(六)串的处理
前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...
- C算法编程题(五)“E”的变换
前言 上一篇<C算法编程题(四)上三角> 插几句话,说说最近自己的状态,人家都说程序员经常失眠什么的,但是这几个月来,我从没有失眠过,当然是过了分手那段时期.每天的工作很忙,一个任务接一个 ...
- C算法编程题(四)上三角
前言 上一篇<C算法编程题(三)画表格> 上几篇说的都是根据要求输出一些字符.图案等,今天就再说一个“上三角”,有点类似于第二篇说的正螺旋,输出的字符少了,但是逻辑稍微复杂了点. 程序描述 ...
- C算法编程题(三)画表格
前言 上一篇<C算法编程题(二)正螺旋> 写东西前还是喜欢吐槽点东西,要不然写的真还没意思,一直的想法是在博客园把自己上学和工作时候整理的东西写出来和大家分享,就像前面写的<T-Sq ...
- C算法编程题(二)正螺旋
前言 上一篇<C算法编程题(一)扑克牌发牌> 写东西前总是喜欢吐槽一些东西,还是多啰嗦几句吧,早上看了一篇博文<谈谈外企涨工资那些事>,里面楼主讲到外企公司包含的五类人,其实不 ...
- C算法编程题(一)扑克牌发牌
前言 上周写<我的编程开始(C)>这篇文章的时候,说过有时间的话会写些算法编程的题目,可能是这两天周末过的太舒适了,忘记写了.下班了,还没回去,闲来无事就写下吧. 因为写C++的编程题和其 ...
- 需掌握 - JAVA算法编程题50题及答案
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? //这是一个菲波拉契数列问题publi ...
随机推荐
- nginx 调优
般来说nginx配置文件中对优化比较有作用的为以下几项:worker_processes 8;1 nginx进程数,建议按照cpu数目来指定,一般为它的倍数.worker_cpu_affinity 0 ...
- [RxJS] AsyncSubject
AsyncSubject emit the last value of a sequence only if the sequence completed. This value is then ca ...
- yii 2.0 代码阅读 小记
1.\yii\base\object 设置了get/set属性...使用getName()获取属性名..构造函数中使用config初始化属性 2.\yii\base\Component 继承自Obje ...
- 如何打开“USB调试”模式?
请首先确认您的系统版本, 点击「设置」-「关于手机」查看您当前的手机版本号. 如果您使用的是 Android 3.2及以下系统,请按以下步骤操作: STEP1:在应用列表选择「设置」进入系统设置菜单: ...
- C语言内存四区
按照老版操作系统来学习,内存对于程序来讲分四区.分别是 代码区,静态区,栈,堆. 由上面程序执行的结果可知: 貌似结果就是 静态代码堆栈 静态区存放的是程序中所有静态变量和常量的值.静态区的大小是程序 ...
- 简单的jquery选择器的实现
function getByClass(oParent,oClass){ if(document.getElementsByClassName){ return document ...
- selendroid inspector xpth元素定位记录
android自动化测试元素定位,目前发现appium官方的uiautomatorviewer一般的元素定位还行,但好多都找不到. 这个时候,可以考虑selendroid的inspector 官网:h ...
- .Net 两个对像之间的映射
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- .NET通过调用Office组件导出Word文档
.NET通过调用Office组件导出Word文档 最近做项目需要实现一个客户端下载word表格的功能,该功能是用户点击"下载表格",服务端将该用户的数据查询出来并生成数据到Word ...
- Ubuntu 16.04 - 64bit 解压 rar 报错 Parsing Filters not supported
Ubuntu 16.04 - 64bit 解压rar 文件报错: 错误如下图: 原因: 未安装解压命令 unrar 参考博客: Error - "Parsing Filters not s ...