2011軟考程序員算法實(shí)例:全排列的遞歸算法

字號(hào):

為了方便廣大考生更好的復(fù)習(xí),幫考網(wǎng)綜合整理提供了2011年軟件水平考試程序員算法實(shí)例:全排列的遞歸算法,以供各位考生考試復(fù)習(xí)參考,希望對(duì)考生復(fù)習(xí)有所幫助。
    using System;
    namespace TotalSort
    {
    /**////
    /// 全排列的遞歸算法
    ///
    class Class1
    {
    /**////
    /// 應(yīng)用程序的主入口點(diǎn)。
    ///
    [STAThread]
    static void Main(string[] args)
    {
    //char[] s = “abcdefghijklmnopqrstuvwxyz”.ToCharArray();
    char[] s = “abcde”.ToCharArray();
    TotalSort(s, 0);
    Console.WriteLine(“\n\n總數(shù):{0}”, resultCount);
    Console.ReadLine();
    }
    static int resultCount = 0;
    public static void TotalSort(char[] list, int start) {
    int end = list.Length - 1;
    if (start == end) {
    resultCount++;
    Console.WriteLine(list);
    }
    else {
    for (int i = start; i 《= end; i++) {
    char[] temp = new char[list.Length];
    list.CopyTo(temp, 0);
    char tempc = temp[start];
    temp[start] = temp[i];
    temp[i] = tempc;
    TotalSort(temp, start + 1);
    }
    }
    }
    }
    }