C#常见基础算法

namespace 面试常见算法
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1 = Test1(5);
            Console.WriteLine(n1);

            Test2();

            Test3();

            int n2 = Test4(9);
            Console.WriteLine(n2);

            string s = Test5();
            Console.WriteLine(s);

            Test6();

            Test7();

            Console.ReadKey();
        }



        #region//1-2+3-4+...+m
        static int Test1(int m)
        {
            int sum = 0;
            for (int i = 1; i <= m; i++)
            {
                if (i % 2 > 0)   //奇偶性判断
                {
                    sum += i;
                }
                else
                {
                    sum -= i;
                }
            }
            return sum;
        }
        #endregion

        #region//1,2,3,4四个数字,能够组成多少互不相同无重复数字的三位数
        static void Test2()
        {
            int count = 0;
            for (int bw = 1; bw <= 4; bw++)
            {
                for (int sw = 1; sw <= 4; sw++)
                {
                    if (sw != bw)
                    {
                        for (int gw = 1; gw <= 4; gw++)
                        {
                            if (gw != sw && gw != bw)
                            {
                                count++;
                                Console.WriteLine("{0}{1}{2}", bw, sw, gw);
                            }
                        }
                    }
                }
            }
            Console.WriteLine("一共有{0}个", count);
        }
        #endregion

        #region//12?56? * 123 = 154?4987
        static void Test3()
        {
            for (int a = 0; a < 10; a++)
            {
                for (int b = 0; b < 10; b++)
                {
                    for (int c = 0; c < 10; c++)
                    {
                        if ((120560 + a + b * 1000) * 123 == 15404987 + c * 10000)
                        {
                            Console.WriteLine(a);
                            Console.WriteLine(b);
                            Console.WriteLine(c);
                        }
                    }
                }
            }
        }
        #endregion

        #region//1 1 2 3 5 8 13 21 34...递归计算第30位数
        static int Test4(int i)
        {
            if (i <= 0)
            {
                return 0;
            }
            else if (i > 0 && i <= 2)
            {
                return 1;
            }
            else
            {
                return Test4(i - 1) + Test4(i - 2);
            }
        }
        #endregion

        #region//字符串反转
        static string Test5()
        {
            string s = "I am a good man";
            string[] arr = s.Split(' ');
            string res = "";
            for (int i = arr.Length - 1; i >= 0; i--)
            {
                res += arr[i];
                if (i > 0)
                    res += " ";
            }
            return res;
        }
        #endregion

        #region//九九乘法表
        static void Test6()
        {
            string t = string.Empty;
            for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    t = string.Format("{0}*{1}={2} ", j, i, (j * i));
                    Console.Write(t);
                    if (i == j)
                        Console.Write("\n");
                }
            }
        }
        #endregion

        #region//冒泡排序 把一组数据按照从大到小/降序或从小到大/升序排列
        static void Test7()
        {
            int[] nums = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
            for (int i = 0; i < nums.Length - 1; i++)
            {
                for (int j = 0; j < nums.Length - 1 - i; j++)
                {
                    if (nums[j] > nums[j + 1])
                    {
                        int temp = nums[j];
                        nums[j] = nums[j + 1];                    //Array.Sort(nums);//升序                                                                            
                        nums[j + 1] = temp;                       //Array.Reverse(nums);//反转
                    }
                }
            }
            for (int i = 0; i < nums.Length; i++)
            {
                Console.Write(nums[i]);
            }
        }
        #endregion

    }
}

 

原文链接:

https://blog.csdn.net/byondocean/article/details/7046101

posted @ 2019-07-05 15:53  _Huang95  阅读(939)  评论(0编辑  收藏  举报