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
  • 相关阅读:
    Surface Mount Package Details
    Boost Converter
    IPC low/medium/high density 什么意思?
    SMT Surface Mount Technology footprint references
    Time Step Too Small in Multisim
    mOByDiC E90C2600 EOBD/OBDII to RS232 gateway
    STN1110 Multiprotocol OBD to UART Interpreter
    STN1170 Multiprotocol OBD to UART Interpreter
    BR16F84 OBD II Interface Chip For PWM, VPW, and ISO 9141-2 Vehicles
    ELM327 OBD to RS232 Interpreters
  • 原文地址:https://www.cnblogs.com/ike_li/p/1554945.html
Copyright © 2011-2022 走看看