C#经典算法面试题汇总

递归算法

使用递归算法来实现计算1+2+3+4+…+100的结果

static int SUM(int x)
{
  if (x <= 1)
    return x;
	else
    return x + SUM(x - 1);
}

一列数的规则如下 : 1 、 1 、 2 、 3 、 5 、 8 、 13 、 21 、 34… 求第 30 位数是多少, 用递归算法实现

static int Foo(int i)
{
    if (i <= 0)
        return 0;
    else if (i > 0 && i <= 2)
        return 1;
    else
        return Foo(i - 1) + Foo(i - 2);
}


static void Main(string[] args)  
{  
    int dataValue = Foo(30);  
    Console.WriteLine(dataValue.ToString());  
}

排序算法

实现一个冒泡排序算法(升序)排序算法

static void Sort(int[] nums)
{
    int temp;
    for (int i = 0; i < nums.Length; i++)
    {
        for (int j = i + 1; j < nums.Length; j++)
        {
            if (nums[i] > nums[j])
            {
                temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
        Console.WriteLine(nums[i]);
    }
}

冒泡排序

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();  
        }  
    }

其他

实现一个方法,对于输入的任意字符串,统计出其中每一种字符出现的次数

/** 字典的定义
 必须包含名空间System.Collection.Generic
 Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)
 键必须是唯一的,而值不需要唯一的
 键和值都可以是任何类型(比如:string, int, 自定义类型,等等)
 通过一个键读取一个值的时间是接近O(1)
 键值对之间的偏序可以不定义
*/
static void CountChar(string str)
{
    Dictionary<char, int> dic = new Dictionary<char, int>();
    foreach (char c in str)
    {
        if (dic.ContainsKey(c))
            dic[c]++;
        else
            dic.Add(c, 1);
    }
    foreach (KeyValuePair<char, int> p in dic)
    {
        Console.WriteLine("字符{0},出现的次数{1}", p.Key.ToString(), p.Value.ToString());
    }
}

实现一个将字符串转换为整数的方法,不要使用int.Parse、int.TryParse、Convert.ToInt32等任何类库方法

               public static bool TryParseToInt(string strData, out int num)
        {
            if (string.IsNullOrWhiteSpace(strData))
            {
                num = 0;
                return false;
            }
            int result = 0;

            bool minus = strData[0] == '-' ? true : false;
            if (minus && strData.Length == 1)
            {
                num = 0;
                return false;
            }

            for (int i = minus ? 1 : 0; i < strData.Length; i++)
            {
                if (strData[i] >= '0' && strData[i] <= '9')
                {
                    result = strData[i] - 48 + result * 10;
                }
                else
                {
                    num = 0;
                    return false;
                }
            }

            num = minus ? -result : result;
            return true;
        }

        static void Main(string[] args)
        {
            var result = TryParseToInt("99", out int getValue);
        }

求以下表达式的值,写出您想到的一种或几种实现方法: 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;  
    }

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值