轉(zhuǎn)載于:C#常見(jiàn)算法面試
一、求以下表達(dá)式的值,寫(xiě)出您想到的一種或幾種實(shí)現(xiàn)方法: 1-2+3-4+……+m
//方法一,通過(guò)順序規(guī)律寫(xiě)程序,同時(shí)也知道flag標(biāo)志位的重要性。
- static int F1(int m)
- {
- int sum =0;
- bool flag =true;
- for (int i = 1; i <= m; i++)
- {
- if (flag) //一次是默認(rèn)是True,下下也為T(mén)rue
- sum += i;
- else
- sum -= i;
- flag = !flag;
-
- }
- return sum;
- }
-
- //通過(guò)奇偶性
- static int F2(int m)
- {
- int sum = 0;
- for (int i = 1; i <= m; i++)
- {
- if (i % 2 >0) //即為奇數(shù)
- sum += i;
- else
- sum -= i;
- }
- return sum;
- }
二、有1、2、3、4個(gè)數(shù)字,能組成多少個(gè)互不相同且無(wú)重復(fù)數(shù)字的三位數(shù)?都是多少?
- class Program
- {
- static void Main(string[] args)
- {
-
- //有1、2、3、4個(gè)數(shù)字,能組成多少個(gè)互不相同且無(wú)重復(fù)數(shù)字的三位數(shù)?都是多少?
- //分解題目
- //條件:四個(gè)數(shù)字1、2、3、4 ;三位數(shù):百位、十位、個(gè)位
- //要求:互不相同;無(wú)重復(fù)數(shù)字:每個(gè)數(shù)字在三位中只出現(xiàn)一次
- //結(jié)果:多少個(gè)? 都是多少?
-
- int count = 0; //統(tǒng)計(jì)個(gè)數(shù)
- for (int bw = 1; bw <= 4; bw++)
- {
- for (int sw = 1; sw <= 4; sw++)
- {
- if (sw!= bw) //很顯然,只有百位和十位不同的情況下才能談個(gè)位。
- {
- for (int gw = 1; gw <= 4; gw++)
- {
- if (gw != sw && gw != bw) //百位用過(guò)的,十位就不能用;百位和十位都用過(guò)的,個(gè)位就不能用
- {
- count++;
- Console.WriteLine("{0}{1}{2}", bw, sw, gw);
- }
- }
- }
- }
- }
- Console.WriteLine("一共有{0}個(gè)", count);
- Console.Read();
-
- }
- }
三、一個(gè)6位數(shù)乘以一個(gè)3位數(shù),得到一個(gè)結(jié)果。但不清楚6位數(shù)的兩個(gè)數(shù)字是什么,而且結(jié)果中有一位數(shù)字也不清楚,請(qǐng)編程找出問(wèn)好代表的數(shù)字,答案可能有多個(gè)。
表達(dá)式:12?56?*123 = 154?4987
- for (int a = 0; a < 10; a++)
- {
- for (int b = 0; b < 10; b++)
- {
- for (int c = 0; c < 10; c++)
- {
- if ((120560 + a + b * 1000) * 123 == 15404987 + c * 10000)
- {
- Console.WriteLine(a);
- Console.WriteLine(b);
- Console.WriteLine(c);
- }
- }
- }
- }
- Console.Read();
四、1、1、1、2、3、5、8、13、21、34,....用C#遞歸寫(xiě)出算法,算出第30個(gè)數(shù)。
- using System;
- class Program
- {
- static in F(int i)
- {
- if(i<=0)
- return 0;
- else if(i>0 && i<=2)
- return 1;
- else return F(i-1) + F(i-2);
- }
-
- static void Main(string[] args)
- {
- int n = F(30);
- Console.WriteLine(n.ToString());
- }
- }
五、有一個(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;
- }
六、C# 九九乘法表算法實(shí)現(xiàn):
- static void Mu()
- {
- string t = string.Empty;
- for (int i = 1; i < 10; i++)
- {
- for (int j = 1; j <= i; j++)
- {
- t = string.Format("{0}*{1}={2} ", j, i, (j * i));
- Console.Write(t);
- //if (j * i < 82)
- // Console.Write(" ");
- if (i == j)
- Console.Write("\n");
- }
- }
- }
七、在1~10000的整數(shù)中,找出同時(shí)符合以下條件的數(shù):a.必須是質(zhì)數(shù)。b.該數(shù)字各位數(shù)字之和為偶數(shù),如數(shù)字12345,各位數(shù)字之和為1+2+3+4+5=15,不是偶數(shù)。
本題考了兩個(gè)地方:
(1)、質(zhì)數(shù)的理解:質(zhì)數(shù)就是所有比1大的整數(shù)中,除了1和它本身外,不再有別的約數(shù)。2是一個(gè)不是奇數(shù)的質(zhì)數(shù),它既是質(zhì)數(shù)也是偶數(shù),面試者極容易忽略這點(diǎn)。判斷數(shù)N是否為質(zhì)數(shù)要直接從3開(kāi)始判斷(如果N不是2),首先不能是偶數(shù),然后再判斷是否能被3、5、7....整除,直到sqrt(N)止。
(2)、求各位數(shù)字之和,可以通過(guò)循環(huán)取余的辦法。
- using System;
- using System.Collections.Generic;
-
- class program
- {
- static void Mian(string[] args)
- {
- int N =1000;
- List<int> primes = new List<int>();
- primes.Add(2);
- Console.Write(2+" ");
- for(int i=3;i<N,i+=2)
- {
- if(!)
-
- }
- }
- static bool IsDigitSumEven(int n)
- {
- int sum=0;
- while(n>0)
- {
- sum +=n% 10;
- n /=10;
- }
- return sum%2==0;
- }
- }
冒泡排序:
- namespace BubbleSorter
- {
- class BubbleSorter
- {
- private static int[] myArray;
- private static int arraySize;
- public static void Sort(int[] a)
- {
- myArray = a;
- arraySize = myArray.Length;
- BubbleSort(myArray);
- }
-
- public static void BubbleSort(int[] myArray)
- {
- for (int i = 0; i < myArray.Length-1; i++) //由于數(shù)組的特點(diǎn),從0開(kāi)始,但myArray的長(zhǎng)度為5,所以需要減1,實(shí)際進(jìn)行了(0~3)4趟循環(huán)
- {
- for (int j =0; j < myArray.Length -1- i; j++) //內(nèi)層循環(huán)的要點(diǎn)是相鄰比較。當(dāng)j=4的時(shí)候,就推出循環(huán)了
- {
- if (myArray[j] > myArray[j + 1])
- {
- Swap(ref myArray[j], ref myArray[j + 1]);
- }
- }
- }
- }
-
- private static void Swap(ref int left, ref int right)
- {
- int temp;
- temp = left;
- left = right;
- right = temp;
- }
-
- static void Main(string[] args)
- {
- int[] a = { 2, 1, 5, 10, 9 };
- BubbleSorter.Sort(a);
- foreach (int i in a)
- {
- Console.WriteLine(i);
- }
- Console.Read();
- }
- }
- }
選擇排序:
選擇排序是一種簡(jiǎn)單直觀的排序算法。它的工作原理如下。
首先在未排序列中找到最小的元素,存放到排序序列的起始位置。然后,在從剩余未排序元素中繼續(xù)尋找最小的元素,放到排序序列末尾。以此類(lèi)推,直到所有元素均排序完畢。
- class SelectSorter
- {
- private static int[] myArray;
- private static int arraySize;
- public static void Sort(int[] a)
- {
- myArray = a;
- arraySize = myArray.Length;
- SelectSort(myArray);
- }
- public static void SelectSort(int[] myArray)
- {
- int i, j, smallest;
- for(i=0;i<myArray.Length-1;i++) //數(shù)據(jù)起始位置,從0到倒數(shù)第二個(gè)數(shù)據(jù)
- {
- smallest = i; //記錄最小數(shù)的下標(biāo)
- for (j = i + 1; j < myArray.Length; j++) //在剩下的數(shù)據(jù)中尋找最小數(shù)
- {
- if (myArray[j] < myArray[smallest]) {
- smallest = j; //如果有比它更小的,記錄下標(biāo)
- }
- }
- Swap(ref myArray[i], ref myArray[smallest]); //將最小數(shù)據(jù)和未排序的第一個(gè)數(shù)交換
- }
- }
-
- private static void Swap(ref int left, ref int right)
- {
- int temp;
- temp = left;
- left = right;
- right = temp;
- }
-
- static void Main(string[] args)
- {
- int[] a = new int[] { 4, 2, 1, 6, 3 };
- SelectSorter.Sort(a);
- for (int i = 0; i < a.Length; i++)
- {
- System.Console.WriteLine(a[i]);
- }
- System.Console.Read();
- }
- }
程序設(shè)計(jì):貓大叫一聲,所有的老鼠都開(kāi)始逃跑,主人被驚醒。
思路:1、構(gòu)造出Cat、Mouse、Master三個(gè)類(lèi),并能使程序運(yùn)行。
2、從Mouse和Master中提取抽象。
3、聯(lián)動(dòng)效應(yīng),只要執(zhí)行Cat.Cryed()就可以使老鼠逃跑,主人驚醒。
通過(guò)這個(gè)例子,可以看出,委托事件的應(yīng)用是極其面向?qū)ο蟮模蛘哒f(shuō)很對(duì)象化!
- namespace DelegateEvent
- {
- public delegate void SubEventHandler();
- public abstract class Subject
- {
- public event SubEventHandler SubEvent;
- protected void FireAway() //開(kāi)火, 抽象類(lèi)可以有具體方法。
- {
- if (this.SubEvent != null)
- this.SubEvent();
- }
- }
-
- public class Cat:Subject
- {
- public void Cry()
- {
- Console.WriteLine("cat cryed.")
- this.FireAway();
- }
- }
-
- public abstract class Observer //定義一個(gè)觀察者的抽象類(lèi),這樣的類(lèi)有一點(diǎn)就是觀察誰(shuí),這個(gè)誰(shuí)肯定是一個(gè)類(lèi),這里指貓
- {
- public Observer(Subject sub) //抽象類(lèi)也可以定義構(gòu)造函數(shù)
- {
- sub.SubEvent +=new SubEventHandler(Respose); //注冊(cè)貓叫事件(表達(dá)有點(diǎn)含糊),當(dāng)此事件觸發(fā)的時(shí)候,老鼠會(huì)做出回應(yīng)
- }
- public abstract void Respose();
- }
-
- //定義一個(gè)觀察者,老鼠
- public class Mouse : Observer
- {
- private string name;
- public Mouse(string name, Subject sub) //定義構(gòu)造函數(shù),并初始化父類(lèi)
- : base(sub)
- {
- this.name = name;
- }
-
- public override void Respose()
- {
- Console.WriteLine(name+" attempt to escape!");
- }
- }
- //定義一個(gè)觀察者,主人
- public class Master : Observer
- {
- public Master(Subject sub) : base(sub) { }
- public override void Respose()
- {
- Console.WriteLine("host waken");
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- Cat cat = new Cat();
- Mouse mouse1 = new Mouse("mouse1", cat); //在對(duì)象初始化的時(shí)候,已經(jīng)注冊(cè)了對(duì)貓叫的響應(yīng)事件
- Mouse mouse2 = new Mouse("mouse2",cat);
- Master master = new Master(cat);
- cat.Cry();
- Console.Read();
- }
- }
- }
|