程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> 排序組合,AS實現窮舉算法

排序組合,AS實現窮舉算法

編輯:關於C

 

原理如圖:

\

 

算法如下:

 

 

 

private function compoud(N:uint, C:uint):void 

        { 

             

            var pickIndex:int = 0; 

            var X:int=1; 

            var Y:int=1; 

            var P:int=1; 

            var i:int=0; 

            var arr:Array  = new Array(); 

            var src:Array  = new Array(); 

            for(i=0;i<N;i++) 

            { 

                src.push(i+1); 

            } 

             

             

            while(true) 

            { 

                if(pickIndex<N) 

                { 

                    arr.push(src[pickIndex++]); 

                    if(arr.length==C) 

                    { 

                        var tmp:String = ""; 

                        for(i=0;i<C;i++)  

                        { 

                            tmp +=arr[i]+ " "; 

                        } 

                        trace(tmp); 

                        if(pickIndex==N) 

                        { 

                            var backto:int =-1; 

                            for(i=C-1;i>0;i--) 

                            { 

                                if(arr[i]-arr[i-1]>1) 

                                { 

                                    backto = i-1; 

                                    break; 

                                } 

                            } 

                            if(backto==-1) 

                            { 

                                break; 

                            } 

                            pickIndex = arr[backto]; 

                            for(i=0;i<C-backto;i++) 

                            { 

                                arr.pop(); 

                            } 

                        } 

                        else 

                        { 

                            arr.pop(); 

                        } 

                    } 

                } 

                else 

                { 

                    break; 

                } 

            } 

        } 

 

摘自 aerror的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved