.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} 毫秒
", (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("
");
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("

{0} 以内共有 {1} 个质数
", 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(); } } }
原文地址:https://www.cnblogs.com/summers/p/3170076.html