日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C#常見(jiàn)算法面試

 liuyans 2017-07-19

轉(zhuǎn)載于:C#常見(jiàn)算法面試


一、求以下表達(dá)式的值,寫(xiě)出您想到的一種或幾種實(shí)現(xiàn)方法: 1-2+3-4+……+m


    //方法一,通過(guò)順序規(guī)律寫(xiě)程序,同時(shí)也知道flag標(biāo)志位的重要性。 
[csharp] view plain copy
  1. static int F1(int m)    
  2.    {    
  3.        int sum =0;    
  4.        bool flag =true;    
  5.        for (int i = 1; i <= m; i++)    
  6.        {    
  7.            if (flag)  //一次是默認(rèn)是True,下下也為T(mén)rue    
  8.                sum += i;    
  9.            else    
  10.                sum -= i;    
  11.            flag = !flag;    
  12.        
  13.        }    
  14.        return sum;    
  15.    }    
  16.        
  17.    //通過(guò)奇偶性    
  18.    static int F2(int m)    
  19.    {    
  20.        int sum = 0;    
  21.        for (int i = 1; i <= m; i++)    
  22.        {    
  23.            if (i % 2 >0)  //即為奇數(shù)    
  24.                sum += i;    
  25.            else    
  26.                sum -= i;    
  27.        }    
  28.        return sum;    
  29.    }    


二、有1、2、3、4個(gè)數(shù)字,能組成多少個(gè)互不相同且無(wú)重復(fù)數(shù)字的三位數(shù)?都是多少?
   
[csharp] view plain copy
  1. class Program    
  2.    {    
  3.        static void Main(string[] args)    
  4.        {    
  5.        
  6.            //有1、2、3、4個(gè)數(shù)字,能組成多少個(gè)互不相同且無(wú)重復(fù)數(shù)字的三位數(shù)?都是多少?    
  7.            //分解題目    
  8.            //條件:四個(gè)數(shù)字1、2、3、4  ;三位數(shù):百位、十位、個(gè)位    
  9.            //要求:互不相同;無(wú)重復(fù)數(shù)字:每個(gè)數(shù)字在三位中只出現(xiàn)一次    
  10.            //結(jié)果:多少個(gè)? 都是多少?    
  11.        
  12.            int count = 0; //統(tǒng)計(jì)個(gè)數(shù)    
  13.            for (int bw = 1; bw <= 4; bw++)    
  14.            {    
  15.                for (int sw = 1; sw <= 4; sw++)    
  16.                {    
  17.                    if (sw!= bw)  //很顯然,只有百位和十位不同的情況下才能談個(gè)位。    
  18.                    {    
  19.                        for (int gw = 1; gw <= 4; gw++)    
  20.                        {    
  21.                            if (gw != sw && gw != bw)   //百位用過(guò)的,十位就不能用;百位和十位都用過(guò)的,個(gè)位就不能用    
  22.                            {    
  23.                                count++;    
  24.                                Console.WriteLine("{0}{1}{2}", bw, sw, gw);    
  25.                            }    
  26.                        }    
  27.                    }    
  28.                }    
  29.            }    
  30.            Console.WriteLine("一共有{0}個(gè)", count);    
  31.            Console.Read();    
  32.        
  33.        }    
  34.    }     

  
三、一個(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
  
[csharp] view plain copy
  1. for (int a = 0; a < 10; a++)    
  2.    {    
  3.        for (int b = 0; b < 10; b++)    
  4.        {    
  5.            for (int c = 0; c < 10; c++)    
  6.            {    
  7.                if ((120560 + a + b * 1000) * 123 == 15404987 + c * 10000)    
  8.                {    
  9.                    Console.WriteLine(a);    
  10.                    Console.WriteLine(b);    
  11.                    Console.WriteLine(c);    
  12.                }    
  13.            }    
  14.        }    
  15.    }    
  16.    Console.Read();    


四、1、1、1、2、3、5、8、13、21、34,....用C#遞歸寫(xiě)出算法,算出第30個(gè)數(shù)。

[csharp] view plain copy
  1. using System;    
  2.    class Program    
  3.    {    
  4.       static in F(int i)    
  5.       {    
  6.           if(i<=0)     
  7.              return 0;    
  8.           else if(i>0 && i<=2)    
  9.              return 1;    
  10.           else return F(i-1) + F(i-2);    
  11.       }    
  12.           
  13.       static void Main(string[] args)    
  14.       {    
  15.           int n = F(30);    
  16.           Console.WriteLine(n.ToString());    
  17.       }    
  18.    }    

五、有一個(gè)字符串 "I am a good man",設(shè)計(jì)一個(gè)函數(shù),返回 "man good a am I"。

[csharp] view plain copy
  1. static string Reverse()    
  2.          {    
  3.              string s = "I am a good man";    
  4.              string[] arr = s.Split(' ');    
  5.              string res = "";    
  6.              for (int i = arr.Length - 1; i >= 0; i--)    
  7.              {    
  8.                  res += arr[i];    
  9.                  if (i > 0)    
  10.                      res += " ";    
  11.              }    
  12.              return res;    
  13.          }    

六、C# 九九乘法表算法實(shí)現(xiàn):

[csharp] view plain copy
  1. static void Mu()    
  2.           {    
  3.               string t = string.Empty;    
  4.               for (int i = 1; i < 10; i++)    
  5.               {    
  6.                   for (int j = 1; j <= i; j++)    
  7.                   {    
  8.                       t = string.Format("{0}*{1}={2} ", j, i, (j * i));    
  9.                       Console.Write(t);    
  10.                       //if (j * i < 82)    
  11.                       //    Console.Write(" ");    
  12.                       if (i == j)    
  13.                           Console.Write("\n");    
  14.                   }    
  15.               }    
  16.           }    


七、在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)取余的辦法。
 
[csharp] view plain copy
  1. using System;    
  2.    using System.Collections.Generic;    
  3.        
  4.    class program    
  5.    {    
  6.       static void Mian(string[] args)    
  7.       {    
  8.          int N =1000;    
  9.          List<int> primes = new List<int>();    
  10.          primes.Add(2);    
  11.          Console.Write(2+" ");    
  12.          for(int i=3;i<N,i+=2)    
  13.          {    
  14.              if(!)    
  15.               
  16.          }    
  17.       }    
  18.       static bool IsDigitSumEven(int n)    
  19.       {    
  20.          int sum=0;    
  21.          while(n>0)    
  22.         {    
  23.             sum +=n% 10;    
  24.             n /=10;    
  25.         }    
  26.         return sum%2==0;    
  27.       }    
  28.    }    


冒泡排序:
[csharp] view plain copy
  1. namespace BubbleSorter    
  2.    {    
  3.        class BubbleSorter    
  4.        {    
  5.            private static int[] myArray;    
  6.            private static int arraySize;    
  7.            public static void Sort(int[] a)    
  8.            {    
  9.                myArray = a;    
  10.                arraySize = myArray.Length;    
  11.                BubbleSort(myArray);    
  12.            }    
  13.        
  14.            public static void BubbleSort(int[] myArray)    
  15.            {    
  16.                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)    
  17.                {    
  18.                    for (int j =0; j < myArray.Length -1- i; j++)  //內(nèi)層循環(huán)的要點(diǎn)是相鄰比較。當(dāng)j=4的時(shí)候,就推出循環(huán)了    
  19.                    {    
  20.                        if (myArray[j] > myArray[j + 1])    
  21.                        {    
  22.                            Swap(ref myArray[j], ref myArray[j + 1]);    
  23.                        }    
  24.                    }    
  25.                }    
  26.            }    
  27.        
  28.            private static void Swap(ref int left, ref int right)    
  29.            {    
  30.                int temp;    
  31.                temp = left;    
  32.                left = right;    
  33.                right = temp;    
  34.            }    
  35.        
  36.            static void Main(string[] args)    
  37.            {    
  38.                int[] a = { 2, 1, 5, 10, 9 };    
  39.                BubbleSorter.Sort(a);    
  40.                foreach (int i in a)    
  41.                {    
  42.                    Console.WriteLine(i);    
  43.                }    
  44.                Console.Read();    
  45.            }    
  46.        }    
  47.    }    

選擇排序:

選擇排序是一種簡(jiǎn)單直觀的排序算法。它的工作原理如下。

首先在未排序列中找到最小的元素,存放到排序序列的起始位置。然后,在從剩余未排序元素中繼續(xù)尋找最小的元素,放到排序序列末尾。以此類(lèi)推,直到所有元素均排序完畢。
  
[csharp] view plain copy
  1. class SelectSorter    
  2.    {    
  3.        private static int[] myArray;    
  4.        private static int arraySize;    
  5.        public static void Sort(int[] a)    
  6.        {    
  7.            myArray = a;    
  8.            arraySize = myArray.Length;    
  9.            SelectSort(myArray);    
  10.        }    
  11.        public static void SelectSort(int[] myArray)     
  12.        {    
  13.            int i, j, smallest;    
  14.            for(i=0;i<myArray.Length-1;i++)  //數(shù)據(jù)起始位置,從0到倒數(shù)第二個(gè)數(shù)據(jù)    
  15.            {    
  16.                smallest = i;            //記錄最小數(shù)的下標(biāo)    
  17.                for (j = i + 1; j < myArray.Length; j++)    //在剩下的數(shù)據(jù)中尋找最小數(shù)    
  18.                {    
  19.                    if (myArray[j] < myArray[smallest]) {    
  20.                        smallest = j;    //如果有比它更小的,記錄下標(biāo)    
  21.                    }    
  22.                }    
  23.                Swap(ref myArray[i], ref myArray[smallest]);   //將最小數(shù)據(jù)和未排序的第一個(gè)數(shù)交換    
  24.            }    
  25.        }    
  26.        
  27.        private static void Swap(ref int left, ref int right)    
  28.        {    
  29.            int temp;    
  30.            temp = left;    
  31.            left = right;    
  32.            right = temp;    
  33.        }    
  34.        
  35.        static void Main(string[] args)    
  36.        {    
  37.            int[] a = new int[] { 4, 2, 1, 6, 3 };    
  38.            SelectSorter.Sort(a);    
  39.            for (int i = 0; i < a.Length; i++)    
  40.            {    
  41.                System.Console.WriteLine(a[i]);    
  42.            }    
  43.            System.Console.Read();    
  44.        }    
  45.    }    


 程序設(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ì)象化!
[csharp] view plain copy
  1. namespace DelegateEvent    
  2. {    
  3.     public delegate void SubEventHandler();    
  4.     public abstract class Subject    
  5.     {    
  6.         public event SubEventHandler SubEvent;    
  7.         protected void FireAway()   //開(kāi)火, 抽象類(lèi)可以有具體方法。    
  8.         {    
  9.             if (this.SubEvent != null)    
  10.                 this.SubEvent();    
  11.         }    
  12.     }    
  13.         
  14.     public class Cat:Subject    
  15.     {    
  16.         public void Cry()    
  17.         {    
  18.             Console.WriteLine("cat cryed.")    
  19.             this.FireAway();    
  20.         }    
  21.     }    
  22.     
  23.     public abstract class Observer  //定義一個(gè)觀察者的抽象類(lèi),這樣的類(lèi)有一點(diǎn)就是觀察誰(shuí),這個(gè)誰(shuí)肯定是一個(gè)類(lèi),這里指貓    
  24.     {    
  25.         public Observer(Subject sub)  //抽象類(lèi)也可以定義構(gòu)造函數(shù)    
  26.         {     
  27.             sub.SubEvent +=new SubEventHandler(Respose);   //注冊(cè)貓叫事件(表達(dá)有點(diǎn)含糊),當(dāng)此事件觸發(fā)的時(shí)候,老鼠會(huì)做出回應(yīng)    
  28.         }    
  29.         public abstract void Respose();     
  30.     }    
  31.         
  32.     //定義一個(gè)觀察者,老鼠    
  33.     public class Mouse : Observer    
  34.     {    
  35.         private string name;    
  36.         public Mouse(string name, Subject sub)  //定義構(gòu)造函數(shù),并初始化父類(lèi)    
  37.             : base(sub)    
  38.         {    
  39.             this.name = name;    
  40.         }    
  41.     
  42.         public override void Respose()    
  43.         {    
  44.             Console.WriteLine(name+" attempt to escape!");    
  45.         }    
  46.     }    
  47.     //定義一個(gè)觀察者,主人    
  48.     public class Master : Observer    
  49.     {    
  50.         public Master(Subject sub) : base(sub) { }    
  51.         public override void Respose()    
  52.         {    
  53.             Console.WriteLine("host waken");    
  54.         }    
  55.     }    
  56.     
  57.     class Program    
  58.     {    
  59.         static void Main(string[] args)    
  60.         {    
  61.             Cat cat = new Cat();    
  62.             Mouse mouse1 = new Mouse("mouse1", cat); //在對(duì)象初始化的時(shí)候,已經(jīng)注冊(cè)了對(duì)貓叫的響應(yīng)事件    
  63.             Mouse mouse2 = new Mouse("mouse2",cat);    
  64.             Master master = new Master(cat);    
  65.             cat.Cry();    
  66.             Console.Read();    
  67.         }    
  68.     }    
  69. }    

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多