C#的常見演算法(面試)

來源:互聯網
上載者:User

標籤:推出   倒數   wap   返回   面試   else   中繼   sum   順序   

一、求以下運算式的值,寫出您想到的一種或幾種實現方法: 1-2+3-4+……+m


    //方法一,通過順序規律寫程式,同時也知道flag標誌位的重要性。

static int F1(int m)       {           int sum =0;           bool flag =true;           for (int i = 1; i <= m; i++)           {               if (flag)  //一次是預設是True,下下也為True                   sum += i;               else                   sum -= i;               flag = !flag;                  }           return sum;       }              //通過奇偶性       static int F2(int m)       {           int sum = 0;           for (int i = 1; i <= m; i++)           {               if (i % 2 >0)  //即為奇數                   sum += i;               else                   sum -= i;           }           return sum;       }    

二,有一個字串 "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;             }    

三.冒泡排序

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++)   //由於數組的特點,從0開始,但myArray的長度為5,所以需要減1,實際進行了(0~3)4趟迴圈                   {                       for (int j =0; j < myArray.Length -1- i; j++)  //內層迴圈的要點是相鄰比較。當j=4的時候,就推出迴圈了                       {                           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();               }           }       }    

四.選擇排序

選擇排序是一種簡單直觀的排序演算法。它的工作原理如下。

首先在未排序列中找到最小的元素,存放到排序序列的起始位置。然後,在從剩餘未排序元素中繼續尋找最小的元素,放到排序序列末尾。以此類推,直到所有元素均排序完畢。

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++)  //資料起始位置,從0到倒數第二個資料               {                   smallest = i;            //記錄最小數的下標                   for (j = i + 1; j < myArray.Length; j++)    //在剩下的資料中尋找最小數                   {                       if (myArray[j] < myArray[smallest]) {                           smallest = j;    //如果有比它更小的,記錄下標                       }                   }                   Swap(ref myArray[i], ref myArray[smallest]);   //將最小資料和未排序的第一個數交換               }           }                  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();           }       }    

五.有1、2、3、4個數字,能組成多少個互不相同且無重複數位三位元?都是多少?

class Program       {           static void Main(string[] args)           {                      //有1、2、3、4個數字,能組成多少個互不相同且無重複數位三位元?都是多少?               //分解題目               //條件:四個數字1、2、3、4  ;三位元:百位、十位、個位               //要求:互不相同;無重複數字:每個數字在三位中只出現一次               //結果:多少個? 都是多少?                      int count = 0; //統計個數               for (int bw = 1; bw <= 4; bw++)               {                   for (int sw = 1; sw <= 4; sw++)                   {                       if (sw!= bw)  //很顯然,只有百位和十位不同的情況下才能談個位。                       {                           for (int gw = 1; gw <= 4; gw++)                           {                               if (gw != sw && gw != bw)   //百位用過的,十位就不能用;百位和十位都用過的,個位就不能用                               {                                   count++;                                   Console.WriteLine("{0}{1}{2}", bw, sw, gw);                               }                           }                       }                   }               }               Console.WriteLine("一共有{0}個", count);               Console.Read();                  }       }     

 

C#的常見演算法(面試)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.