算法的具体应用,举几个实例

解决方案 »

  1.   


    指的是这样一个数列:
        1,1,2,3,5,8,13,21……
    这个数列从第三项开始,每一项都等于前两项之和。可将其递归定义为:
    using System;
    using System.Collections.Generic;
    using System.Text; 
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int n = 1;
                int n1 = 1;
                int n2 = 2;
                System.Console.WriteLine(n + ""+ n1 + "");
                for (int i = 0; i < 10;i++ )
                {
                    n2 = n + n1;
                    System.Console.WriteLine(n2 + "");
                    n = n1;
                    n1 = n2;
                }
                System.Console.WriteLine();
            }
        }
    }
    选择排序  
    class SelectionSorter  
    {  
        private int min;  
        public void Sort(int[] arr)  
        {  
            for (int i = 0; i < arr.Length - 1; ++i)  
            {  
                min = i;  
                for (int j = i + 1; j < arr.Length; ++j)  
                {  
                    if (arr[j] < arr[min])  
                        min = j;  
                }  
                int t = arr[min];  
                arr[min] = arr[i];  
                arr[i] = t;  
            }  
        }  
        static void Main(string[] args)  
        {  
            int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };  
            SelectionSorter s = new SelectionSorter();  
            s.Sort(array);  
            foreach (int m in array)  
                Console.WriteLine("{0}", m);  
        }  
    }  
    冒泡排序  
    class EbullitionSorter  
    {  
        public void Sort(int[] arr)  
        {  
            int i, j, temp;  
            bool done = false;  
            j = 1;  
            while ((j < arr.Length) && (!done))//判断长度  
            {  
                done = true;  
                for (i = 0; i < arr.Length - j; i++)  
                {  
                    if (arr[i] > arr[i + 1])  
                    {  
                        done = false;  
                        temp = arr[i];  
                        arr[i] = arr[i + 1];//交换数据  
                        arr[i + 1] = temp;  
                    }  
                }  
                j++;  
            }  
        }  
     
        static void Main(string[] args)  
        {  
            int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };  
            EbullitionSorter e = new EbullitionSorter ();  
            e.Sort(array);  
            foreach (int m in array)  
                Console.WriteLine("{0}", m);  
     
        }  
    }  
    快速排序  
    class QuickSorter  
    {  
        private void swap(ref int l, ref int r)  
        {  
            int temp;  
            temp = l;  
            l = r;  
            r = temp;  
        }  
        public void Sort(int[] list, int low, int high)  
        {  
            int pivot;//存储分支点  
            int l, r;  
            int mid;  
            if (high <= low)  
                return;  
            else if (high == low + 1)  
            {  
                if (list[low] > list[high])  
                    swap(ref list[low], ref list[high]);  
                return;  
            }  
            mid = (low + high) >> 1;  
            pivot = list[mid];  
            swap(ref list[low], ref list[mid]);  
            l = low + 1;  
            r = high;  
            do 
            {  
                while (l <= r && list[l] < pivot)  
                    l++;  
                while (list[r] >= pivot)  
                    r--;  
                if (l < r)  
                    swap(ref list[l], ref list[r]);  
            } while (l < r);  
            list[low] = list[r];  
            list[r] = pivot;  
            if (low + 1 < r)  
                Sort(list, low, r - 1);  
            if (r + 1 < high)  
                Sort(list, r + 1, high);  
        }     
        static void Main(string[] args)  
        {  
            int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };  
            QuickSorter q = new QuickSorter();  
            q.Sort(iArrary, 0, 13);  
            for (int m = 0; m <= 13; m++)  
                Console.WriteLine("{0}", iArrary[m]);  
        }  
    }  
    插入排序  
    public class InsertionSorter  
    {  
        public void Sort(int[] arr)  
        {  
            for (int i = 1; i < arr.Length; i++)  
            {  
                int t = arr[i];  
                int j = i;  
                while ((j > 0) && (arr[j - 1] > t))  
                {  
                    arr[j] = arr[j - 1];//交换顺序  
                    --j;  
                }  
                arr[j] = t;  
            }  
        }  
        static void Main(string[] args)  
        {  
            int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };  
            InsertionSorter i = new InsertionSorter();  
            i.Sort(array);  
            foreach (int m in array)  
                Console.WriteLine("{0}", m);   
        }  
    }  
    希尔排序  
    public class ShellSorter  
    {  
        public void Sort(int[] arr)  
        {  
            int inc;  
            for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ;  
            for (; inc > 0; inc /= 3)  
            {  
                for (int i = inc + 1; i <= arr.Length; i += inc)  
                {  
                    int t = arr[i - 1];  
                    int j = i;  
                    while ((j > inc) && (arr[j - inc - 1] > t))  
                    {  
                        arr[j - 1] = arr[j - inc - 1];//交换数据  
                        j -= inc;  
                    }  
                    arr[j - 1] = t;  
                }  
            }  
        }  
     
        static void Main(string[] args)  
        {  
            int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };  
            ShellSorter s = new ShellSorter();  
            s.Sort(array);  
            foreach (int m in array)  
                Console.WriteLine("{0}", m);      
        }  

      

  2.   

    冒泡排序using System;namespace BubbleSorter{ public class BubbleSorter{ public void Sort(int [] list){ int i,j,temp;bool done=false;j=1;while((j<list.Length)&&(!done)){ done=true;for(i=0;i<list.Length-j;i++){if(list[i]>list[i+1]){done=false;temp=list[i];list[i]=list[i+1];list[i+1]=temp;} }j++; }} }public class MainClass{ public static void Main(){int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};BubbleSorter sh=new BubbleSorter();sh.Sort(iArrary);for(int m=0;m<iArrary.Length;m++)Console.Write("{0} ",iArrary[m]);Console.WriteLine(); 
    http://www.mscto.com} }}
    选择排序
    using System;namespace SelectionSorter{ public class SelectionSorter{ private int min;public void Sort(int [] list){ for(int i=0;i<list.Length-1;i++){ min=i;for(int j=i+1;j<list.Length;j++){ if(list[j]<list[min])min=j;}int t=list[min];list[min]=list[i];list[i]=t;} }}public class MainClass{ public static void Main(){int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};SelectionSorter ss=new SelectionSorter();ss.Sort(iArrary);for(int m=0;m<iArrary.Length;m++)Console.Write("{0} ",iArrary[m]);Console.WriteLine();} }}插入排序using System; namespace InsertionSorter{ public class InsertionSorter{ public void Sort(int [] list){ for(int i=1;i<list.Length;i++){ int t=list[i];int j=i;while((j>0)&&(list[j-1]>t)){ list[j]=list[j-1];--j;}list[j]=t; }}}public class MainClass{ public static void Main(){int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};InsertionSorter ii=new InsertionSorter();ii.Sort(iArrary);for(int m=0;m<iArrary.Length;m++)Console.Write("{0}",iArrary[m]);Console.WriteLine();} }}希尔排序using System;namespace ShellSorter{public class ShellSorter{http://www.mscto.compublic void Sort(int [] list){int inc;for(inc=1;inc<=list.Length/9;inc=3*inc+1);for(;inc>0;inc/=3){for(int i=inc+1;i<=list.Length;i+=inc){int t=list[i-1];int j=i;while((j>inc)&&(list[j-inc-1]>t)){list[j-1]=list[j-inc-1];j-=inc;}list[j-1]=t;} }} }public class MainClass{ public static void Main(){int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};ShellSorter sh=new ShellSorter();sh.Sort(iArrary);for(int m=0;m<iArrary.Length;m++)Console.Write("{0} ",iArrary[m]);Console.WriteLine();} }}
      

  3.   

    递归算法
    1.一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
    答:public class MainClass 

    public static void Main() 

    Console.WriteLine(Foo(30)); 

    public static int Foo(int i) 

    if (i <= 0) 
    return 0; 
    else if(i > 0 && i <= 2) 
    return 1; 
    else return Foo(i -1) + Foo(i - 2); 

    } 以上是C#的代码,下面是asp.net(vb.net代码)    Function FunFoo(ByVal V As Integer) As Long
            If (V <= 0) Then
                FunFoo = 0
            ElseIf (V > 0 And V <= 2) Then
                FunFoo = 1
            Else
                FunFoo = FunFoo(V - 1) + FunFoo(V - 2)
            End If
        End Function2. 求5的阶乘。。(java写的代码)   
    public class Test {  
    static int multiply(int n){  
    if(n==1||n==0)  
    return n;  
    else  
    return n*multiply(n-1);  
    }  public static void main(String[] args){  
    System.out.println(multiply(10));  
    }  
    } 3.(转帖)情景是:一元一瓶的汽水,2个空瓶能换一瓶汽水。 
    问假如你有X元钱,能喝Y瓶汽水. 用y=f(x)表示
    如果是N空瓶换一个空瓶呢,用y=f(x,n)表示我的代码,如果是2换1:
    package lihan; 
    public class test { 
        public static Integer digui(int x) 
        { 
            if(x==1) 
            { 
                return x; 
            } 
            if(x%2==1) 
            return x+digui(x/2)+1; 
            else 
                return x+digui(x/2); 
                } 
        /** 
         * @param args 
         */ 
        public static void main(String[] args) { 
            // TODO 自动生成方法存根 
            int i=digui(11); 
            System.out.print(i);     } }如果是N换1的话,那么
    public class lihan { 
        public static Integer digui(int x,int n) 
        { 
            if(x==1) 
            { 
                return x; 
            } 
            if(x%n==(n-1)) 
            return x+digui(x/n,n)+1; 
            else 
                return x+digui(x/n,n); 
                } 
        /** 
         * @param args 
         */ 
        public static void main(String[] args) { 
            // TODO 自动生成方法存根 
            int i=digui(12,3)+1; 
            System.out.print(i); 
        } } 4.java冒泡排序算法(转帖) package lihan; public class wordTest 

        public static void main(String args[]) 
        { 
            int[] m = 
            { 2, 8, 43, 3, 33 }; 
            int[] n = sort(m); 
            for (int i = 0; i < m.length; i++) 
            { 
                System.out.println(n[i] + "\n"); 
            }     }     /* 冒泡排序算法 */ 
        public static int[] sort(int[] m) 
        { 
            int intLenth = m.length; 
            /*执行intLenth次*/ 
            for (int i = 0; i < intLenth; i++) 
            { 
                /*每执行一次,将最小的数排在后面*/ 
                for (int j = 0; j < intLenth - i - 1; j++) 
                { 
                    int a = m[j]; 
                    int b = m[j + 1]; 
                    if (a < b) 
                    { 
                        m[j] = b; 
                        m[j + 1] = a; 
                    } 
                } 
            } 
            return m;     } 
    } 编程实现一个冒泡排序算法?(C#程序代码)
    int [] array = new int ;
    int temp = 0 ;
    for (int i = 0 ; i < array.Length - 1 ; i++)
    {
    for (int j = i + 1 ; j < array.Length ; j++)
    {
    if (array[j] < array[i])
    {
    temp = array[i] ;
    array[i] = array[j] ;
    array[j] = temp ;
    }
    }
    }
      

  4.   

    编程之美里就很多
    二叉树遍历
     class Program
        {
          class nodes<T>
            {
                T data;
                nodes<T> Lnode, Rnode, Pnode;
                public T Data
                {
                    set { data = value; }
                    get { return data; }            }
                public nodes<T> LNode
                {
                    set { Lnode = value; }
                    get { return Lnode; }
                }
                public nodes<T> RNode
                {
                    set { Rnode = value; }
                    get { return Rnode; }            }            public nodes<T> PNode
                {
                    set { Pnode = value; }
                    get { return Pnode; }            }
              public nodes()
              { }
              public nodes(T data)
              {
                  this.data = data;
              }        } 
            static void PreOrder<T>(nodes<T> rootNode)
            {
                if (rootNode != null)
                {
                    Console.WriteLine(rootNode.Data);
                    PreOrder<T>(rootNode.LNode);
                    PreOrder<T>(rootNode.RNode);            }
            }
            static nodes<string> BinTree()
            {
                nodes<string>[] binTree = new nodes<string>[8];
                binTree[0] = new nodes<string>("A");
                binTree[1] = new nodes<string>("B");
                binTree[2] = new nodes<string>("C");
                binTree[3] = new nodes<string>("D");
                binTree[4] = new nodes<string>("E");
                binTree[5] = new nodes<string>("F");
                binTree[6] = new nodes<string>("G");
                binTree[7] = new nodes<string>("H");            binTree[0].LNode = binTree[1];
                binTree[0].RNode = binTree[2];
                binTree[1].RNode = binTree[3];
                binTree[2].LNode = binTree[4];
                binTree[2].RNode = binTree[5];
                binTree[3].LNode = binTree[6];
                binTree[3].RNode = binTree[7];
                return binTree[0];
            }
            static void MidOrder<T>(nodes<T> rootNode)
            {
                if (rootNode != null)
                {
                    MidOrder<T>(rootNode.LNode);
                    Console.WriteLine(rootNode.Data);
                    MidOrder<T>(rootNode.RNode);
                }
            } 
            static void AfterOrder<T>(nodes<T> rootNode)
            {
                if (rootNode != null)
                {
                    AfterOrder<T>(rootNode.LNode);
                    AfterOrder<T>(rootNode.RNode);
                    Console.WriteLine(rootNode.Data);
                }        } 
            static void LayerOrder<T>(nodes<T> rootNode)
            {
                nodes<T>[] Nodes = new nodes<T>[20];
                int front = -1;
                int rear = -1;
                if (rootNode != null)
                {
                    rear++;
                    Nodes[rear] = rootNode;            }            while (front != rear)
                {
                    front++;
                    rootNode = Nodes[front];
                    Console.WriteLine(rootNode.Data);
                    if (rootNode.LNode != null)
                    {
                        rear++;
                        Nodes[rear] = rootNode.LNode;
                    }
                    if (rootNode.RNode != null)
                    {
                        rear++;
                        Nodes[rear] = rootNode.RNode;
                    }
                }
            }    }