分享

C#常见算法面试

 liuyans 2017-07-19

转载于:C#常见算法面试


一、求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m


    //方法一,通过顺序规律写程序,同时也知道flag标志位的重要性。 
[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)  //一次是默认是True,下下也为True    
  8.                sum += i;    
  9.            else    
  10.                sum -= i;    
  11.            flag = !flag;    
  12.        
  13.        }    
  14.        return sum;    
  15.    }    
  16.        
  17.    //通过奇偶性    
  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)  //即为奇数    
  24.                sum += i;    
  25.            else    
  26.                sum -= i;    
  27.        }    
  28.        return sum;    
  29.    }    


二、有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
   
[csharp] view plain copy
  1. class Program    
  2.    {    
  3.        static void Main(string[] args)    
  4.        {    
  5.        
  6.            //有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?    
  7.            //分解题目    
  8.            //条件:四个数字1、2、3、4  ;三位数:百位、十位、个位    
  9.            //要求:互不相同;无重复数字:每个数字在三位中只出现一次    
  10.            //结果:多少个? 都是多少?    
  11.        
  12.            int count = 0; //统计个数    
  13.            for (int bw = 1; bw <= 4; bw++)    
  14.            {    
  15.                for (int sw = 1; sw <= 4; sw++)    
  16.                {    
  17.                    if (sw!= bw)  //很显然,只有百位和十位不同的情况下才能谈个位。    
  18.                    {    
  19.                        for (int gw = 1; gw <= 4; gw++)    
  20.                        {    
  21.                            if (gw != sw && gw != bw)   //百位用过的,十位就不能用;百位和十位都用过的,个位就不能用    
  22.                            {    
  23.                                count++;    
  24.                                Console.WriteLine("{0}{1}{2}", bw, sw, gw);    
  25.                            }    
  26.                        }    
  27.                    }    
  28.                }    
  29.            }    
  30.            Console.WriteLine("一共有{0}个", count);    
  31.            Console.Read();    
  32.        
  33.        }    
  34.    }     

  
三、一个6位数乘以一个3位数,得到一个结果。但不清楚6位数的两个数字是什么,而且结果中有一位数字也不清楚,请编程找出问好代表的数字,答案可能有多个。

表达式: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#递归写出算法,算出第30个数。

[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.    }    

五、有一个字符串 "I am a good man",设计一个函数,返回 "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# 九九乘法表算法实现:

[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的整数中,找出同时符合以下条件的数:a.必须是质数。b.该数字各位数字之和为偶数,如数字12345,各位数字之和为1+2+3+4+5=15,不是偶数。

本题考了两个地方:

(1)、质数的理解:质数就是所有比1大的整数中,除了1和它本身外,不再有别的约数。2是一个不是奇数的质数,它既是质数也是偶数,面试者极容易忽略这点。判断数N是否为质数要直接从3开始判断(如果N不是2),首先不能是偶数,然后再判断是否能被3、5、7....整除,直到sqrt(N)止。

(2)、求各位数字之和,可以通过循环取余的办法。
 
[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++)   //由于数组的特点,从0开始,但myArray的长度为5,所以需要减1,实际进行了(0~3)4趟循环    
  17.                {    
  18.                    for (int j =0; j < myArray.Length -1- i; j++)  //内层循环的要点是相邻比较。当j=4的时候,就推出循环了    
  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.    }    

选择排序:

选择排序是一种简单直观的排序算法。它的工作原理如下。

首先在未排序列中找到最小的元素,存放到排序序列的起始位置。然后,在从剩余未排序元素中继续寻找最小的元素,放到排序序列末尾。以此类推,直到所有元素均排序完毕。
  
[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++)  //数据起始位置,从0到倒数第二个数据    
  15.            {    
  16.                smallest = i;            //记录最小数的下标    
  17.                for (j = i + 1; j < myArray.Length; j++)    //在剩下的数据中寻找最小数    
  18.                {    
  19.                    if (myArray[j] < myArray[smallest]) {    
  20.                        smallest = j;    //如果有比它更小的,记录下标    
  21.                    }    
  22.                }    
  23.                Swap(ref myArray[i], ref myArray[smallest]);   //将最小数据和未排序的第一个数交换    
  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.    }    


 程序设计:猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。

思路:1、构造出Cat、Mouse、Master三个类,并能使程序运行。

2、从Mouse和Master中提取抽象。

3、联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒。

通过这个例子,可以看出,委托事件的应用是极其面向对象的,或者说很对象化!
[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()   //开火, 抽象类可以有具体方法。    
  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  //定义一个观察者的抽象类,这样的类有一点就是观察谁,这个谁肯定是一个类,这里指猫    
  24.     {    
  25.         public Observer(Subject sub)  //抽象类也可以定义构造函数    
  26.         {     
  27.             sub.SubEvent +=new SubEventHandler(Respose);   //注册猫叫事件(表达有点含糊),当此事件触发的时候,老鼠会做出回应    
  28.         }    
  29.         public abstract void Respose();     
  30.     }    
  31.         
  32.     //定义一个观察者,老鼠    
  33.     public class Mouse : Observer    
  34.     {    
  35.         private string name;    
  36.         public Mouse(string name, Subject sub)  //定义构造函数,并初始化父类    
  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.     //定义一个观察者,主人    
  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); //在对象初始化的时候,已经注册了对猫叫的响应事件    
  63.             Mouse mouse2 = new Mouse("mouse2",cat);    
  64.             Master master = new Master(cat);    
  65.             cat.Cry();    
  66.             Console.Read();    
  67.         }    
  68.     }    
  69. }    

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多