dim xxx
dim Temp
xxx = Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)Temp = Array(3,6,14,1,5,18)Temp数组中的元素是随机的,但范围在1-20间,也就是XXX的子集要求xxx删除temp中的元素,得到新的一个数组···怎样的算法最好?我用算法比较差,用了 词典 dim xxx As New Dictionary ,添加修改都非常方便。
但人家都说数组存储快,上面20个元素只是举例子,事实不止20个···

解决方案 »

  1.   

    先给一个简单的算法,见代码:
    Dim xxx
    Dim Temp
    Dim result()
    xxx = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)Temp = Array(3, 6, 14, 1, 5, 18)Dim i, j, count
    Dim isDelete As Booleancount = 0 '用来计数
    For i = 0 To UBound(xxx)
        isDelete = False
        For j = 0 To UBound(Temp)
            If Temp(j) = xxx(i) Then '让temp中的数据与xxx的对比是否存在
                isDelete = True
                Exit For
            End If
        Next j
        
        '如果没有在temp中找到数值,则将其加入新数组
        If Not isDelete Then
            ReDim Preserve result(count)
            result(count) = xxx(i)
            count = count + 1
        End If
    Next i'遍历结果
    For i = 0 To UBound(result)
        Debug.Print result(i)
    Next i
      

  2.   

    如果xxx数组是排过序的话,你也可以用除2法去算,即将temp数组中的每个数去从xxx数组长度的一半开始比对,如果大于xxx数组中的一半的大小,那么你在从xxx大于temp中数字的那一半在除以2,直到找到等于的为止。(小于类似)
      

  3.   

    楼上的,字典需要引用一个 runtime 啊··它的remove功能是不错,但是add的功能就一般了
      

  4.   

    要是链表不用移动数据还可以,要是移动数据就不好了!
    xxx = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
    Temp = Array(3, 6, 14, 1, 5, 18)
    dim newxxx() as integer
    for i = 0 to ubound(Temp)
       xxx(Temp(i)-1)= 0
    nextredim newxxx(20-ubound(temp)) as integer
    dim Count as  integer
    for i = 0 to 19
      if xxx(i)<> 0 then
         newxxx(count) = xxx(i)
         count++
      end if
    next