html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

C#非递归方式实现排列组合_.NET教程_编程技术

[ ] 已经帮助:人解决问题

C#以非递归方式实现三位排列组合,如下代码:K72HTML5中文学习网 - HTML5先行者学习网

  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.     }  
K72HTML5中文学习网 - HTML5先行者学习网
K72HTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助