C#常見算法面試題小結(jié)
本文實(shí)例匯總了C#面試常見的算法題及其解答。具有不錯(cuò)的學(xué)習(xí)借鑒價(jià)值。分享給大家供大家參考。具體如下:
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.有一列數(shù)1,1,2,3,5,........求第30個(gè)數(shù).
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. 程序設(shè)計(jì): 貓大叫一聲,所有的老鼠都開始逃跑,主人被驚醒。
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.有一個(gè)字符串 "I am a good man",設(shè)計(jì)一個(gè)函數(shù),返回 "man good a am I"。
static string Reverse()
{
string s = "I am a good man";
string[] arr = s.Split(' ');
string res = "";
for (int i = arr.Length - 1; i >= 0; i--)
{
res += arr[i];
if (i > 0)
res += " ";
}
return res;
}
5.A、B、C、D、E五名學(xué)生有可能參加計(jì)算機(jī)競(jìng)賽,根據(jù)下列條件判斷哪些人參加了競(jìng)賽:
(1)A參加時(shí),B也參加;
(2)B和C只有一個(gè)人參加;
(3)C和D或者都參加,或者都不參加;
(4)D和E中至少有一個(gè)人參加;
(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;
}
相信本文所述對(duì)大家的C#程序設(shè)計(jì)有一定的借鑒價(jià)值。
相關(guān)文章
C#SuperSocket的搭建并配置啟動(dòng)總結(jié)
在本篇文章里我們給大家總結(jié)了關(guān)于C#SuperSocket的搭建并配置啟動(dòng)的相關(guān)內(nèi)容,正在學(xué)習(xí)的朋友們跟著參考下。2019-05-05
c#模擬js escape方法的簡(jiǎn)單實(shí)例
這篇文章主要介紹了c#模擬js escape方法的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-11-11
C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法,結(jié)合實(shí)例形式總結(jié)分析了Json轉(zhuǎn)換DataTable,以及DataTable導(dǎo)出Excel相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
C#使用TreeView控件實(shí)現(xiàn)的二叉樹泛型節(jié)點(diǎn)類及其方法
TreeView?控件在?C#?中主要用于顯示分層結(jié)構(gòu)的數(shù)據(jù),這通常是一個(gè)文件系統(tǒng)的表示,但也可以是任何具有父子關(guān)系的數(shù)據(jù)集合,本文給大家介紹了C#使用TreeView控件實(shí)現(xiàn)的二叉樹泛型節(jié)點(diǎn)類及其方法,需要的朋友可以參考下2024-03-03
C#實(shí)現(xiàn)Windows服務(wù)測(cè)試與調(diào)試
這篇文章介紹了C#實(shí)現(xiàn)Windows服務(wù)測(cè)試與調(diào)試的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
C#將圖片和字節(jié)流互相轉(zhuǎn)換并顯示到頁(yè)面上
本文主要介紹用C#實(shí)現(xiàn)圖片轉(zhuǎn)換成字節(jié)流,字節(jié)流轉(zhuǎn)換成圖片,并根據(jù)圖片路徑返回圖片的字節(jié)流,有需要的朋友可以參考下2015-08-08

