zoukankan      html  css  js  c++  java
  • 算法题汇集

    1。冒泡排序算法(Bubble Sort)

    冒泡排序基本思想
    将n个记录看作按纵向排列,每趟排序时自下至上对每对相邻记录进行比较,若次序不符合要求(逆序)就交换。每趟排序结束时都能使排序范围内关键字最小的记录象一个气泡一样升到表上端的对应位置,整个排序过程共进行n-1趟,依次将关键字最小、次小、第三小…的各个记录“冒到”表的第一个、第二个、第三个…位置上。

       初态      第1趟   第2趟  第3趟  第4趟  第5趟  第6趟  第7趟
        38        12      12      12      12      12      12      12                              
        20        38      20      20      20      20      20      20
        46        20      38      25      25      25      25      25
        38        46      25      38      38      38      38      38
        74        38      46      38      38      38      38      38
        91        74      38      46      46      46      46      46
        12        91      74      74      74      74      74      74
        25        25      91      91      91      91      91      91
     

    int[] myArray = { 9, 7, 6, 8, 5, 4 };
                int itemp;
                int arrLength = myArray.Length - 1;
                bool isExchange = false;
                for (int i = 0; i < arrLength; i++)
                {
                    isExchange = false;
                    for (int j = arrLength; j >i; j--)
                    {
                        if (myArray[j - 1] > myArray[j])
                        {
                            itemp = myArray[j];
                            myArray[j] =myArray[j-1];
                            myArray[j - 1] = itemp;
                            isExchange = true;
                        }
                    }
                    if (!isExchange) //本趟排序未发生交换,提前终止算法 
                    { break; }
                }
                label1.Text = "";
                foreach (int strSort in myArray)
                {
                    label1.Text = label1.Text + strSort.ToString() + ",";
                    //Response.Write(strSort.ToString() + ",");
                }

    2.字母排序

         把"baby,abc,decf,eza,ef, Abc, dec"排序成"abc,Abc,baby,dec,decf,ef,eza"

     规则1.按照字母的升序排列。

          2.相同的字母,大写字母放在小写字母后面。 

         3.前一位字母大小写相同,按后一位字母比较大小。

    Code

      比较的类

    Code

    2.17个人围成一圈,从第一个人开始报数,报到3的退出,一直到剩下最后一个人
    Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
        
    class Person
        {
            
    private int _index;
            
    private int _id;
            
    public Person(int index, int id)
            {
                
    this._index = index;
                
    this._id = id;
            }
            
    public int Index
            {
                
    get { return _index; }
                
    set { _index = value; }
            }
            
    public int Id
            {
                
    get { return _id; }
                
    set { _id = value; }
            }
          
        }
        
    }
    Code

    3.一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。(斐波那契数列)

    Code
  • 相关阅读:
    EntityFramework优缺点
    领导者与管理者的区别
    七个对我最好的职业建议(精简版)
    The best career advice I’ve received
    Difference between Stored Procedure and Function in SQL Server
    2015年上半年一次通过 信息系统项目管理师
    Difference between WCF and Web API and WCF REST and Web Service
    What’s the difference between data mining and data warehousing?
    What is the difference between a Clustered and Non Clustered Index?
    用new创建函数的过程发生了什么
  • 原文地址:https://www.cnblogs.com/ike_li/p/1554945.html
Copyright © 2011-2022 走看看