首页 > 编程 > .NET > 正文

C#.NET非递归方式实现排列组合

2024-07-10 12:54:49
字体:
来源:转载
供稿:网友
C#以非递归方式实现三位排列组合
本站收录这篇文章C#.NET非递归方式实现排列组合,详细解说文章中相关NET 递归 排列 技术与知识,欢迎能给大家一些在这方面的支持和帮助!下面是详细内容:

C#以非递归方式实现三位排列组合,如下代码:

 
  1. //深度优先 
  2. class Program 
  3.     { 
  4.         static void Main(string[] args) 
  5.         { 
  6.             int[] number = new int[] { 1, 3, 5, 7 }; 
  7.             List data = new  List(); 
  8.             Stack openStack = new Stack(); 
  9.             Tree root = new Tree(); 
  10.             Tree parent =root; 
  11.             while (true
  12.             { 
  13.  
  14.                 if (parent.GetDeep() == 4) 
  15.                 { 
  16.                     parent.printf(); 
  17.  
  18.                 } 
  19.                 else 
  20.                 { 
  21.                    var tempSon= number.ToList(); 
  22.                    foreach (var item in tempSon) 
  23.                    { 
  24.                        Tree Node = new Tree(); 
  25.                        Node.NodeData = item; 
  26.                        Node.Parent = parent; 
  27.                        openStack.Push(Node); 
  28.                    } 
  29.                 } 
  30.                if (openStack.Count == 0) 
  31.                     break
  32.                var itemData= openStack.Pop(); 
  33.                parent = itemData; 
  34.  
  35.             } 
  36.             System.Console.Read(); 
  37.         } 
  38.         public static void printf(List data) 
  39.         { 
  40.             string d=""; 
  41.             data.ForEach(p => d = d + p); 
  42.             System.Console.WriteLine(d); 
  43.         } 
  44.     } 
  45.     class Tree 
  46.     { 
  47.         public Tree Parent; 
  48.         public int NodeData; 
  49.         public List Son = new List(); 
  50.         public int GetDeep() 
  51.         { 
  52.             int i=0; 
  53.               var p=this
  54.             while (true
  55.             { 
  56.                 if (p == null
  57.                 { 
  58.                     return i; 
  59.                 } 
  60.                 else 
  61.                 { 
  62.                     p = p.Parent; 
  63.                     i++; 
  64.                 
  65.                 } 
  66.  
  67.             } 
  68.  
  69.         } 
  70.         public void printf() 
  71.         { 
  72.             string pf = ""; 
  73.             var p = this
  74.             while (true
  75.             { 
  76.                 if (p == null
  77.                 { 
  78.                     System.Console.WriteLine(pf); 
  79.                     return
  80.                 } 
  81.                 else 
  82.                 { 
  83.                     if (p.NodeData != 0) 
  84.                     { 
  85.                         pf = p.NodeData + pf; 
  86.                     } 
  87.                     p = p.Parent; 
  88.                 } 
  89.             } 
  90.         
  91.         } 
  92.     } 
  93. //广度优先 
  94. class Program 
  95.     { 
  96.         static void Main(string[] args) 
  97.         { 
  98.             int[] number = new int[] { 1, 3}; 
  99.             List<int> data = new  List<int>(); 
  100.             Stack<Tree> openStack = new Stack<Tree>(); 
  101.             Queue<Tree> openQueue = new Queue<Tree>(); 
  102.  
  103.             Tree root = new Tree(); 
  104.             Tree parent =root; 
  105.             while (true
  106.             { 
  107.               
  108.                 if (parent.GetDeep() == 4) 
  109.                 { 
  110.                     parent.printf(); 
  111.  
  112.                 } 
  113.                 else 
  114.                 { 
  115.                    var tempSon= number.ToList(); 
  116.                    foreach (var item in tempSon) 
  117.                    { 
  118.                        Tree Node = new Tree(); 
  119.                        Node.NodeData = item; 
  120.                        Node.Parent = parent; 
  121.                       // openStack.Push(Node); 
  122.                        openQueue.Enqueue(Node); 
  123.                    } 
  124.                 } 
  125.                 if (openQueue.Count == 0) //if (openStack.Count == 0) 
  126.                     break
  127.                var itemData = openQueue.Dequeue(); //openStack.Pop(); 
  128.                parent = itemData; 
  129.             } 
  130.             System.Console.Read(); 
  131.         } 
  132.         public static void printf(List<int> data) 
  133.         { 
  134.             string d=""
  135.             data.ForEach(p => d = d + p); 
  136.             System.Console.WriteLine(d); 
  137.         } 
  138.     } 
  139.     class Tree 
  140.     { 
  141.         public Tree Parent; 
  142.         public int NodeData; 
  143.         public List<Tree> Son = new List<Tree>(); 
  144.         public int GetDeep() 
  145.         { 
  146.             int i=0; 
  147.               var p=this
  148.             while (true
  149.             { 
  150.                 if (p == null
  151.                 { 
  152.                     return i; 
  153.                 } 
  154.                 else 
  155.                 { 
  156.                     p = p.Parent; 
  157.                     i++; 
  158.  
  159.                 } 
  160.  
  161.             } 
  162.         
  163.         } 
  164.         public void printf() 
  165.         { 
  166.             string pf = ""
  167.             var p = this
  168.             while (true
  169.             { 
  170.                 if (p == null
  171.                 { 
  172.                     System.Console.WriteLine(pf); 
  173.                     return
  174.                 } 
  175.                 else 
  176.                 { 
  177.                     if (p.NodeData != 0) 
  178.                     { 
  179.                         pf = p.NodeData + pf; 
  180.                     } 
  181.                     p = p.Parent; 
  182.                 } 
  183.             }  
  184.         } 
  185.     }  
教程内容到此结束,欢迎您再次访问http://www.VeVb.com,我们致力于提供丰富的web开发教程及资源。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表