zoukankan      html  css  js  c++  java
  • c#常用算法

    /// <summary>
        ///  1+2+3+....+n的递归算法
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static int Process1(int i)
        {
            //计算1+2+3+4+...+100的值
     
            if (i == 0) return 1;
            if (i == 1) return 1;
            return Process1(i - 2) + Process1(i - 1);
        }
        /// <summary>
        /// 1+2+3+....+n的非递归算法
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static int Process2(int i)
        {
            //计算1+2+3+4+...+100的值
     
            if (i == 0) return 0;
            return Process2(i - 1) + i;
        }
     
        /// <summary>
        /// 1-2+3-4+5-....+n的非递归算法
        /// </summary>
        /// <param name="isum"></param>
        /// <param name="itype"></param>
        /// <returns></returns>
        public static int Process0(int isum, int itype)
        {
            int sum = 0;
     
            for (int i = 1; i <= isum; i++)
            {
                if (itype == 1)
                {
                    if (i % 2 != 0)
                    {
                        sum += i;
                    }
                    else
                    {
                        sum += (-1) * i;
                    }
                }
                else
                {
                    sum += i;
                }
            }
            return sum;
        }
        /// <summary>
        /// 冒泡法排序
        /// </summary>
        /// <param name="arrLen"></param>
        public static void order1(ref int[] arrLen)
        {
            int temp;
            for (int i = 0; i < arrLen.Length; i++)//冒泡法排序
                for (int j = i + 1; j < arrLen.Length; j++)
                    if (arrLen[i] > arrLen[j])
                    {
                        temp = arrLen[i];
                        arrLen[i] = arrLen[j];
                        arrLen[j] = temp;
                    }
        }
     
        /// <summary>
        /// 冒泡法排序
        /// </summary>
        /// <param name="arrLen"></param>
        public static void order2(ref int[] arrLen)
        {
            for (int i = 0; i < arrLen.Length - 1; i++)
            {
                for (int j = 0; j < arrLen.Length - 1 - i; j++)
                {
                    if (arrLen[j] > arrLen[j + 1])
                    {
                        int temp = arrLen[j];
                        arrLen[j] = arrLen[j + 1];
                        arrLen[j + 1] = temp;
                    }
                }
            }
        }
     
         
        //折半查找、二分算法
        //数组必须按照一定的顺序
        //参数:最大,最小,目标(参数类型为整数)
        public static int BinarySearch(int min, int max, int num)
        {
            if (min == max) return -1;
            int mid = (min + max) / 2;
            if (a[mid] == num) return mid;
            else if (a[mid] < num)
            {
                return BinarySearch(mid + 1, max, num);
            }
            else
            {
                return BinarySearch(min, mid - 1, num);
            }
        }
        //折半查找、二分算法
        //非递归算法
        public static int BinarySearch_F(int num)
        {
            int min = 0;
            int max = 9;
            int mid;
            while (min <= max)
            {
                mid = (min + max) / 2;
                if (a[mid] == num) return mid;
                else if (a[mid] > num) max = mid - 1;
                else min = mid + 1;
            }
            return -1;
        }<br> int a[10] ={1,5,10,13,17,23,65,77,81,93}
  • 相关阅读:
    JAVA中 ReentrantReadWriteLock读写锁详系教程,包会
    传统企业的精益转型之路
    什么时候可以使用极限编程?
    “懒蚂蚁”效应在产品开发过程中的应用
    Vue.config.js配置 最新可用版本
    如何查找一个为NULL的MYSQL字段
    MYSQL 50 基础题 (转载)
    记录一下第一次写 50行 SQL代码
    jwtUtils顾名思意
    对于我们程序员来说,基本面是什么呢?
  • 原文地址:https://www.cnblogs.com/xiaogui9527/p/2982266.html
Copyright © 2011-2022 走看看