.NET面試常考算法


通過這幾天面試經驗來看,發先如下算法考的概率較大。
所以整理如下,供大家參考!

1.求質數
   質數也成為素數,質數就是這個數除了1和他本身兩個因數以外,沒有其他因數的數,叫做質數,和他相反的是合數,
   就是除了1和他本身兩個因數以外,還友其他因數的數叫做合數。

 1  namespace ConsoleApp
 2  {
 3       class Program
 4       {
 5          static void Main(string[] args)
 6          {
 7              long i;
 8              while (true)
 9              {
10                  Console.Write("請輸入要計算的質數(0退出):");
11                  i = long.Parse(Console.ReadLine());
12                  if (i == 0) break;
13                  DateTime t1 = DateTime.Now;
14                  switch (i)
15                  {
16                      case 1: Console.WriteLine("1 不是質數!"); break;
17                      case 2: Console.WriteLine("2 是質數!"); break;
18                      default: cal(i); break;
19                  }
20                  DateTime t2 = DateTime.Now;
21                  Console.WriteLine("時間為:{0} 毫秒\n", (t2 - t1).Ticks / 10000f);
22              }
23          }
24  
25       //以下為函數部分    
26       static void cal(long x)
27       {
28            long sum = 1;
29            byte row = 1;
30            Console.Write("\n");
31            for (long a = 3; a < x + 1; a++)
32            {
33               bool flag = true;
34                for (long b = 2; b < (a / 2) + 1; b++)
35                {
36                    if (a % b != 0) continue;
37                    flag = false;
38                    break;
39                }
40                 if (flag)
41                 {
42                    if (row == 10) { Console.WriteLine(); row = 0; }
43                    if (sum == 1) Console.Write("{0,7}", 2);
44                    Console.Write("{0,7}", a);
45                    sum++; row++;
46                 }
47             }
48             Console.WriteLine("\n\n{0} 以內共有 {1} 個質數\n", x, sum);
49        }
50      }
51  }

2.有一列數1,1,2,3,5,........求第30個數.

 1   public static int Foo(int i)
 2     {
 3        if (i <= 0)
 4             return 0;
 5        else if (i > 0 && i <= 2)
 6             return 1;
 7        else
 8             return  Foo(i - 1) + Foo(i - 2);
 9     }
10  

 3.冒泡排序

 1  //冒泡排序類
 2   public class sorter
 3   {
 4         public void Sort(int[] list)
 5         {
 6             int i, j, temp;
 7             bool done = false;
 8             j = 1;
 9             while ((j < list.Length) && (!done))
10             {
11                 done = true;
12                 for (i = 0; i < list.Length - j; i++)
13                 {
14                     if (list[i] > list[i + 1])
15                     {
16                         done = false;
17                         temp = list[i];
18                         list[i] = list[i + 1];
19                         list[i + 1] = temp;
20                     }
21                 }
22                 j++;
23             }
24         }
25     }
26 
27 //調用代碼
28  class Program
29  {
30         static void Main(string[] args)
31         {
32             int[] arrary = new int[] { 1, 5, 15, 19, 34, 55, 54, 2, 97, 13, 34, 100, 79, 22 };
33             sorter sh = new sorter();
34             sh.Sort(arrary);
35             for (int i = 0; i < arrary.Length; i++)
36             {
37                 Console.Write(arrary[i]);
38                 Console.Write(",");
39             }
40 
41             Console.ReadKey();
42         }
43     }

 4.請編寫一個函數,能夠計算10以內數的階乘,盡量采用遞歸算法。(10!=3628800)。

1 public int jiecheng(int n)
2 {
3        if (n == 1)
4           return 1;
5        else if (n == 2)
6            return 2;
7         else
8            return n * jiecheng(n - 1);
9 }

5 請編程實現此方法。將輸入的整型數組,合並轉換為逗號分隔的字符串。

   例如輸入參數為整型數組{972},那么輸出結果為字符串"9,7,2" 

1 private static string Combine(int[] data)
2   {
3       string str = "";
4       foreach (int s in data)
5       {
6            str += s.ToString() + ",";
7       }
8       return str;
9   }

 6.產生一個int數組,長度為100,並向其中隨機插入1-100,並且不能重復。

 1 //產生一個int數組,長度為100,並向其中隨機插入1-100,並且不能重復。
 2             int[] arr = new int[100];
 3             ArrayList myList = new ArrayList();
 4             Random rad = new Random();
 5             while (myList.Count < 100)
 6             {
 7                 int num = rad.Next(1, 101);
 8                 if (!myList.Contains(num))
 9                 {
10                     myList.Add(num);
11                 }
12             }
13             for (int i = 0; i < 100; i++)
14             {
15                 arr[i] = (int)myList[i];
16             }
17             for (int i = 0; i < arr.Length; i++)
18             {
19                 Console.Write(arr[i] + ",");
20             }
21             Console.ReadKey();

 7.請將字符串"I am a student"按單詞逆序輸出 如"student a am I"

string S = "I am a student";
char[] C = new char[] { ' '};
string[] n =S.Split(C);
int length = S.Length;
for (int i =length-1 ; i >=0; i--)
{
        Console.Write(n[i]);
        if (i != 0)
        {
            Console.Write(" ");
        }
}

 8.C# 取兩個數組的相同元素

摘要: 以往我們都是肯定絞盡腦汁,肯定什么循環,元素大小,什么因素都考慮進去。但是現在采用Linq可以很好的解決這個問題。找出兩個或多個數組的相同項。代碼如下:
usingSystem;
 usingSystem.Collections.Generic;
 usingSystem.Linq;
 usingSystem.Text;
 namespaceTest4_03
 {
    classProgram
     {
        staticvoidMain(string[] args)
         {
            string[] names = {"Adams","Arthur","Buchanan","Tsbuchis","ShCian","FuchsiaLinda","DecheChen","Lotheer","FindLanciCade",
"SorchLand","JiangZheng","MisiiLoda","Gtod","Dfac","Lama","BakCades","Losangle","ZheWQ","GehengDahaLothi","ToryLandey",
"DakaLothy","BthLanda","MenNorth","Fith","FoxMain","DontM","Saobba","Del","Sala","Ghero","BhthLaPhda"}; IEnumerable<string> skip = names.Skip(10); IEnumerable<string> take = names.Take(11); //取出兩個序列中交集部分,按理論應該輸出JiangZheng IEnumerable<string> intersect = skip.Intersect(take); foreach(varsinintersect) { Console.WriteLine(s); } Console.ReadKey(); } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM