如果我要一个数列中随机一个数,并且去掉数列中的那个数,该怎么做呢

解决方案 »

  1.   

    作业贴,给个提示
    dim n as long
    n = 10
    dim a1(1 to n) as long '定义原来的数组,n个元素
    dim a2() as long '删除之后的数组
    dim i as long '循环变量
    '为了测试,给原来的数组填充数据
    for i = 1 to ubound(a1)
        a(i) = i
    next
    '定义随机下标
    dim r as long
    '初始化随机数发生器
    randomize
    '获取一个1~n的随机数
    r = int(rnd() * (ubound(a1) + 1))
    'a2包含1~n-1个元素
    redim a2(1 to ubound(a1) - 1)
    'a2(r)之前的元素从a1()复制
    for i = 1 to r - 1
        a2(i) = a1(i)
    next
    '跳过a1(r),复制后面的,a1(r)等于删除了
    for i = r to ubound(a1) - 1
        a2(i) = a1(i - 1)
    next
    '输出结果
    debug.print print "after:"
    for i = 1 to ubound(a2)
        debug.print a2(i);
    next给LZ一个思考题:数组复制效率比较低,怎么优化下?