zoukankan      html  css  js  c++  java
  • 没事练习一下算法:全排列的递归算法。

    using System;

    namespace TotalSort
    {
        
    /// <summary>
        
    /// 全排列的递归算法
        
    /// </summary>

        class Class1
        
    {
            
    /// <summary>
            
    /// 应用程序的主入口点。
            
    /// </summary>

            [STAThread]
            
    static void Main(string[] args)
            
    {
                
    //char[] s = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
                char[] s = "abcde".ToCharArray();
                TotalSort(s, 
    0);            
                Console.WriteLine(
    "\n\n总数:{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);
                    }

                }

            }

        }

    }


    本来想测试 a - z 的全排列,但估算了一下数目相当惊人,只好作罢。
    (这个数目是 26!)

    采用了递归仅仅是为了锻炼算法,效率肯定是很低的。
  • 相关阅读:
    【Linux笔记】Linux目录结构
    《Effective C#》快速笔记(五)-
    《Effective C#》快速笔记(四)- 使用框架
    《Effective C#》快速笔记(三)- 使用 C# 表达设计
    《Effective C#》快速笔记(二)- .NET 资源托管
    《Effective C#》快速笔记(一)- C# 语言习惯
    Visual Studio 数据库架构比较
    C# 反射与dynamic最佳组合
    C# 调用WebApi
    基于微软开发平台构建和使用私有NuGet托管库
  • 原文地址:https://www.cnblogs.com/RChen/p/178268.html
Copyright © 2011-2022 走看看