C#常见算法面试题小结
程序员文章站
2023-12-16 22:50:46
本文实例汇总了c#面试常见的算法题及其解答。具有不错的学习借鉴价值。分享给大家供大家参考。具体如下:
1.写出冒泡,选择,插入排序算法。
//冒泡排序...
本文实例汇总了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"。
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五名学生有可能参加计算机竞赛,根据下列条件判断哪些人参加了竞赛:
(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#程序设计有一定的借鉴价值。