【C#】常见的程序面试考题

废话不多说,程序实务直接开干!


学习目标: 程序面试的实务

学习难度: ☆☆☆


Binary Search

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int BinarySearch(int[] array,int target)
        {
            int min = 0;

            int max = array.Length - 1;

            while(min<=max)
            {
                int mid = (min + max) / 2;

                if (target == array[mid]) return mid;

                if (target < array[mid]) max = mid - 1;

                if (target > array[mid]) min = mid + 1;
            }

            return -1;
        }

        static void Main(string[] args)
        {
            int[] array = { 1, 3, 5, 7, 9 };

            Console.WriteLine(BinarySearch(array, 9));
        }
    }
}

Fibonacci

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Fibonacci(int num)
        {
            if (num == 0) return 0;

            if (num == 1) return 1;

            return Fibonacci(num-1)+Fibonacci(num-2);
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Fibonacci(13));
        }
    }
}

DownStairs

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        public static long DownStairs(int n)
        {
            if(n==1)
            {
                return 1;
            }
            else if(n==2)
            {
                return 2;
            }
            else
            {
                return DownStairs(n - 2) + DownStairs(n - 1);
            }
        }

        public static void Main(string[] args)
        {
            Console.WriteLine(DownStairs(4));
        }
    }
}

Maximum Subarray

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        public static int GetMax(int[] array)
        {
            int sum = 0;

            int max = array[0];

            for (int i = 0; i < array.Length; ++i)
            {
                sum += array[i]; //计算阵列中连续数的总和(0~4 ~ 4~4)

                sum = Math.Max(0, sum); //排除掉负数的加总

                max = Math.Max(sum, max); //判断当前与上一个加总哪个较大
            }
            return max;
        }

        static void Main(string[] args)
        {
            int[] array = {1,7,-70,30,30};

            Console.WriteLine(GetMax(array));
        }
    }
}

Bubble Sort

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        public static int[] BubbleSort(int[] array)
        {
            int temp;

            for (int i = 0; i < array.Length; ++i)
            {
                for (int j = i+1; j < array.Length; ++j)
                {
                    if(array[i]>array[j])
                    {                     
                        temp = array[j];

                        array[i] = array[j];

                        array[j] = temp;
                    }
                }              
            }
            return array;
        }

        static void Main(string[] args)
        {
            int[] array = {3,1,7,2,4,9};

            array = BubbleSort(array);

            foreach (var item in array)
            {
                Console.WriteLine(item+"\n");
            }
        }
    }
}

Swap two variables without temp

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static void Main(string[] args)
        {
            int a = 5;

            int b = 10;
            
            //XOR做运算

            a = a ^ b;

            b = a ^ b;

            a = a ^ b;

            Console.WriteLine("a"+a+","+"b"+b);
        }
    }
}

sum the Integers (1 to N)

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Sum(int last)
        {
            int first = 1;

            return  (first+last)*last/ 2;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Sum(100));
        }
    }
}

sum the Integers (1x2+2x3+3x4+...(n-1)xn)

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Sum(int n)
        {
            int sum=0;

            for (int i = 2; i < n; i++)
            {
                sum += (i - 1) * i;
            }

            return sum;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Sum(100));
        }
    }
}

sum the Integers (1X1+2X2+3X3+...+nxn)

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Sum(int n)
        {
            int sum=0;

            for (int i = 1; i <= n; i++)
            {
                sum += i * i;
            }

            return sum;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Sum(100));
        }
    }
}

Find a max number in unsort array

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int FindMax(int[] array)
        {
            int max = 0;

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] > max) max = array[i];
            }

            return max;
        }

        static void Main(string[] args)
        {
            int[] array = new int[] { 1, 5, 7, 11, 2, 4, 9, 20, 23, 8, 13, 30, 6 };

            Console.WriteLine(FindMax(array));
        }
    }
}

Find a second max number in unsort array

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int FindSecondMax(int[] array)
        {
            int max= 0;

            int second = 0;

            for (int i = 0; i < array.Length; i++)
            {

                int temp = array[i];

                if (temp > max)
                {
                    second = max; max = temp; 
                }

                else if (temp > second) second = temp;

            }

            return second;
        }

        static void Main(string[] args)
        {
            int[] array = new int[] { 1, 5, 7, 11, 2, 4, 9, 20, 23, 8, 13, 30, 6 };

            Console.WriteLine(FindSecondMax(array));
        }
    }
}

Find the missing integer in array

using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int Sum(int[] array)
        {
            int sum=0;

            for (int i = 0; i < array.Length; i++)
            {
                sum += array[i];
            }

            return sum;
        }

        static void Main(string[] args)
        {
            int[] array = new int[] { 1,2,3,4,6,7,8 };

            int result = ((array[0] + array[array.Length-1]) * (array.Length + 1) / 2) - Sum(array);

            Console.WriteLine(result);
        }
    }
}

Find the larger number without judgement statements

using System;

namespace ConsoleApp1
{
    public class MainProgram
    {
        static int max(int a, int b)
        {
            return ((a + b) + Math.Abs((a - b))) / 2;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(max(1, 3));
        }
    }
}

参考资料:

https://emn178.pixnet.net/blog/post/92389195


<<:  自主学习Android_APP开发 #纪录1

>>:  【C++】Data Type Size Of

JS 20 - HTML 字串算什麽,教你用 CSS 选择器建立网页元素!

大家好! 昨天我们建立了 insert 方法,今天我们就要延伸它的用法。 我们进入今天的主题吧! 前...

Day1-JavaScript(JS)与TypeScript(TS)的基本观念

Hi~开赛第一天先来简单了解一下JavaScript(JS)与TypeScript(TS)的基本观念...

[DAY20]跟 Vue.js 认识的30天 - Vue 插件(Plugin)

这一篇说实在的,现在的我还不能写出有用的插件,所以这一篇笔记主要也是学习使用别人写好的插件。 使用插...

Day 19 - 语音情绪辨识简介

语言除了能够传达字面上的讯息之外,也蕴含了说话者所要表达的情绪,情绪的展现能够让对方更清楚的了解讯息...

[Day9] Face Detection - 使用OpenCV & Dlib:Haar cascades

了解一套工具最好的方法是:动手完成一个现有的范例 了解一门技术最好的方法是:用那套技术完整做出一个...