怎样选择一个数列中的随机项并在数列中去掉这个项呢
如果我要一个数列中随机一个数,并且去掉数列中的那个数,该怎么做呢
[解决办法]
作业贴,给个提示
dim n as longn = 10dim a1(1 to n) as long '定义原来的数组,n个元素dim a2() as long '删除之后的数组dim i as long '循环变量'为了测试,给原来的数组填充数据for i = 1 to ubound(a1) a(i) = inext'定义随机下标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