C#常见算法题目(面试准备)
1.写出冒泡,选择,插入排序算法。
//冒泡排序
public class bubblesorter
{
public void sort(int[] list)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < list.Length) && (!done))
{
done = true;
for (i = 0; i < list.Length - j; i++)
{
if (list[i] > list[i + 1])
{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
j++;
}
}
}
//选择排序
public class selectionsorter
{
private int min;
public void sort(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;
}
}
}
//插入排序
public class insertionsorter
{
public void sort(int[] list)
{
for (int i = 1; i < list.Length; i++)
{
int t = list[i];
int j = i;
while ((j > 0) && (list[j - 1] > t))
{
list[j] = list[j - 1];
--j;
}
list[j] = t;
}
}
}2.有一列数1,1,2,3,5,........求第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);
}
}
3. 程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。
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();
} 
} 4.有一个字符串 "I am a good man",设计一个函数,返回 "man good a am I"。
5.A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些
人参加了竞赛:
(1)A参加时,B也参加;
(2)B和C只有一个人参加;
(3)C和D或者都参加,或者都不参加;
(4)D和E中至少有一个人参加;
(5)如果E参加,那么A和D也都参加。
static void Main(string[] args)
{
char[] name={'A','B','C','D','E'};
int[] value = new int[5];
for (value[0]=0;value[0]<2;value [0]++)
for (value[1]=0; value[1] < 2; value[1]++)
for (value[2]=0; value[2] < 2; value[2]++)
for (value[3]=0; value[3] < 2; value[3]++)
for (value[4]=0; value[4] < 2; value[4]++)
{
if ((value[1] >= value[0]) && (value[1] + value[2] == 1) && (value[2] == value[3]) && (value[3] + value[4]==1) && (value[4]==0 || value[4]==1 && value[0]==1 && value[3]==1))
{
for (int i = 0; i < 5; i++)
{
if (value[i]==1)
{
Console.WriteLine("{0}参加", name[i]);
}
else
{
Console.WriteLine("{0}不参加", name[i]);
}
}
}
}
}6.题目:
a user entered an integer value into a text box. Without using a buit-in library, convert the numeric string to its integer representation.
static int StringTolnt(string s)
{
int sum = 0;
for (int i = 0; i < s.Length; i++)
sum = sum * 10 + (s[i] - '0');
return sum;
}C#常见算法题目(面试准备)的更多相关文章
- C#的常见算法(面试)
一.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+--+m //方法一,通过顺序规律写程序,同时也知道flag标志位的重要性. static int F1(int m) { ; ...
- C#的常见算法(面试)(转)
一.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m //方法一,通过顺序规律写程序,同时也知道flag标志位的重要性. static int F1(int m) { ; ...
- JS常见算法题目
最近收集了几个经典JS题目,比较有代表性,分享一下: 1.xiaoshuo-ss-sfff-fe 变为驼峰xiaoshuoSsSfffFe function getCamelCase(str ...
- C#常见算法题目
//冒泡排序 public class bubblesorter { public void sort(int[] list) { ...
- Top Coder算法题目浏览器
作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/top-code-offline-browser/ 关于 左耳朵耗子 ...
- iOS面试中常见的算法题目
一.前言 这里是在iOS求职中自己遇到的算法题,希望对大家有所帮助.不定期更新.如果大家想在线运行代码调试,可以将代码拷贝到这里.然后进行调试.下面就是常见的算法题目. 二.正文 1.就n的阶乘.(这 ...
- 19道常见的JS面试算法题
最近秋招也做了多多少少的面试题,发现除了基础知识外,算法还是挺重要的.特意整理了一些常见的算法题,添加了自己的理解并实现. 除此之外,建议大家还可以刷刷<剑指offer>.此外,左神在牛客 ...
- 字符串匹配常见算法(BF,RK,KMP,BM,Sunday)
今日了解了一下字符串匹配的各种方法. 并对sundaysearch算法实现并且单元. 字符串匹配算法,是在实际工程中经常遇到的问题,也是各大公司笔试面试的常考题目.此算法通常输入为原字符串(strin ...
- 如何准备算法工程师面试,斩获一线互联网公司机器学习岗offer?
原文:https://zhuanlan.zhihu.com/p/76827460?utm_source=wechat_session&utm_medium=social&utm_oi= ...
随机推荐
- 推荐几本C#程序员阅读的书籍
http://www.cnblogs.com/tongming/p/3879752.html
- android中实现“再按一次退出”功能
首先,定义两次点击退出按钮的时间间隔:private static final long INTERNAL_TIME=2000; 然后,定义一个当前时间的变量:private long exitTim ...
- oracle linux下oracle 10g启动EM、isqlplus及相关命令语法
转载:http://hancang2000.blog.sohu.com/139239421.html ORACLE_HOME/bin/emctl start dbconsole $ORACLE_HOM ...
- 24种设计模式--状态模式【State Pattern】
现在城市发展很快,百万级人口的城市一堆一堆的,那其中有两个东西的发明在城市的发展中起到非常重要的作用:一个是汽车,一个呢是...,猜猜看,是什么?是电梯!汽车让城市可以横向扩展,电梯让城市可以纵向延伸 ...
- WebConfig加密解密
加密:aspnet_regiis -pef appSettings "G:\FlyMusicNew\Web"解密:aspnet_regiis -pdf appSettings &q ...
- 在类库或winform项目中打开另一个winform项目的窗体
假设类库或winform项目为A,另一个winform项目为B.那麽在A中添加一个接口,里面有一个Show方法,然后在B中写一个类b继承这个接口,并重写这个方法,具体内容为弹出某个窗体.然后在A中另一 ...
- event的属性
t获取鼠标相对于浏览器左上角的坐标 <div id="dv" style=" width:300px; height:200px; background-color ...
- word中几个好用的宏代码(立方米上标、关闭样式自动更新、删除无效样式、表格加粗边框、宋体引号)
Sub 替换立方米() With Selection.Find .Text = "m3" .Replacement.Text = "mm3" .Forward ...
- TDirectory.IsEmpty判断指定目录是否为空
使用函数: System.IOUtils.TDirectory.IsEmpty class function IsEmpty(const Path: string): Boolean; static; ...
- python 数据运算
算数运算: