参与的Pb算法:5个数的排列组合。

来源:互联网 发布:淘宝实拍保护在哪里 编辑:程序博客网 时间:2024/05/17 01:15
原贴 地址:http://community.csdn.net/Expert/TopicView3.asp?id=5338869

题如下:用1、2、2、3、4、5这六个数字,用pb写一个函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.

我的算法:



Char lc_point[6] = {'1','2','2','3','4','5'}
Char lc_temp
Int  i,j,k,l
String ls_points[]
//l = 0
Do
    If lc_point[3] = '4' Or Pos(lc_point,"35") > 0 Or Pos(lc_point,"53") > 0 Then
    Else
        l ++
        ls_points[l] = lc_point       
        //dw_1.Object.#1[dw_1.InsertRow(0)] = lc_point
    End If
    //下一个
    For i = 5 To 1 Step -1
        If lc_point[i] < lc_point[i + 1] Then Exit
    Next
    //
    j = 6 - i
    If i > 0 Then
        //lc_point = Left(lc_point,i) + Reverse(Right(lc_point,j))   
        //下面的Choose语句是上面的变体,为了速度
        Choose Case j
            Case 1
                lc_temp = lc_point[6]
                lc_point[6] = lc_point[5]
                lc_point[5] = lc_temp
            Case 2
                lc_temp = lc_point[6]
                If lc_temp > lc_point[4] Then
                    lc_point[6] = lc_point[5]
                    lc_point[5] = lc_point[4]
                    lc_point[4] = lc_temp                   
                Else       
                    lc_point[6] = lc_point[4]
                    lc_point[4] = lc_point[5]                       
                    lc_point[5] = lc_temp
                End If               
            Case Else                           
                k = i + 1
                j = 6
                do
                    lc_temp = lc_point[j]
                    lc_point[j] = lc_point[k]
                    lc_point[k] = lc_temp
                    k ++
                    j --
                Loop While k < j
                //如果搞个二分查找,可能会快些
                lc_temp = lc_point[i]
                For k = i + 1 To 6
                    If lc_point[k] > lc_temp Then
                        lc_point[i] = lc_point[k]
                        lc_point[k] = lc_temp
                        Exit
                    End If
                Next
        End Choose
    Else
        Exit
    End If   
Loop While True

i = dw_1.RowCount() + 1
//If i = 0 Then i = 1
dw_1.Object.#1[i,i + l - 1] = ls_points


思路:由于是由字符组成的字符串,字符是固定的,所以我使用了 首先把字符排序(从小到大),然后递增(把大的数排到前面去,小的数挪到后面去,同时保证为递增的方式),最后为从大到小,这样我们就达到了组合的目的了。然后对特殊的几种组和进行判断就好。


发现把  一个数组中的数据填入DW一列用dw_1.OBject.#1.Current或者我上面的,真快。几乎不占用多少时间。
原创粉丝点击