zoukankan      html  css  js  c++  java
  • C# 基础·算法篇

    1、冒泡排序

      排序

    int[] ArrayList = new int[] {81,23,66,34,99,77,98 };
                for (int i = 0; i < ArrayList.Count(); i++)
                {
                    for (int j = i; j < ArrayList.Count(); j++)
                    {
                        int temp = 0;
                        if (ArrayList[i]>ArrayList[j])
                        {
                            temp = ArrayList[j];
                            ArrayList[j] = ArrayList[i];
                            ArrayList[i] = temp;
                        }
                    }
                }
    View Code

    2、递归算法

      一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少。

    public static int RecursiveAlgorithm(int num)
            {
                int numOut=0;
                if (num==1||num==2)
                {
                    numOut = 1;
                }
                else
                {
                    numOut = RecursiveAlgorithm(num-1) + RecursiveAlgorithm(num - 2);
                }
                return numOut;
            }
    View Code

     3、字符串反转

    public static string Sort1(int[] num)
            {
                int temp;
                for (int i = 0; i < num.Length; i++)
                {
                    for (int j = i+1; j < num.Length; j++)
                    {
                        if (num[i]<num[j])
                        {
                            temp = num[j];
                            num[j] = num[i];
                            num[i] = temp;
                        }
                    }
                }
                StringBuilder sb = new StringBuilder();
                foreach (var item in num)
                {
                    sb.Append(item+"|");
                }
                return sb.ToString();
            }
    
            public static string Sort2(int[] num)
            {
                int temp;
                for (int i = 0; i < num.Length; i++)
                {
                    temp = num[i];
                    int j = i;
                    while (j > 0 && num[j - 1] > temp)
                    {  //通过盘点,值一次次提前
                        num[j] = num[j - 1];
                        --j;
                    }
                    num[j] = temp;
                }
                StringBuilder sb = new StringBuilder();
                foreach (var item in num)
                {
                    sb.Append(item + "|");
                }
                return sb.ToString();
            }
    
            public static string test1(string str)
            {
                Char[] arr = str.ToCharArray();
                Array.Reverse(arr);
                return new string(arr);
            }
            public static string Test2(string str)
            {
                int length = str.Length;
                char[] chr = new char[length];
                for (int i = 0; i < length; i++)
                {
                    chr[i] =str[length-i-1];
                }
                return new string(chr);
            }
    
            public static string Test3(string str)
            {
                StringBuilder sb = new StringBuilder(str.Length);
                for (int i = 0; i < str.Length; i++)
                {
                    sb.Append(str[str.Length-i-1]);
                }
                return sb.ToString();
            }
    View Code
  • 相关阅读:
    Elasticsearch 类比 mysql 实现 in and like or
    es 全文查询
    es 聚合查询
    es多字段分组并求数量
    es 多字段分组并求和
    es 滚动查询二
    es 滚动查询一
    java8 日期操作
    语录(心灵鸡汤来一波)
    并发处理-隔离级别
  • 原文地址:https://www.cnblogs.com/eric-gms/p/5600505.html
Copyright © 2011-2022 走看看