| 贵 州 学 习 网 |
|
1、算法说明数组中元素的插入和删除一般是在已固定序列的数组中插入或删除一个元素,使得插入或删除操作后的数组还是有序的。 代码如下: [NextPage] 2、实战练习 1) 补充代码(2001秋二(8)) c盘根目录下文件data4.txt的内容是:2,4,6,8,10,1,3,5,7,9。下面程序的功能是将文件后半部分的奇数分别按序插入到前半部分的适当位置,得到的新数列是:1 2 3 4 5 6 7 8 9 10。(实现方法:第一次调整后的数列是:1 2 4 6 8 10 3 5 7 9第二次调整后的数列是:1 2 3 4 6 8 10 5 7 9)。 option explict private sub form_click() dim a(10) as integer,i as integer,j as integer open "c:\data4.txt" for input as #12 do (1) j=j+1 input #12,a(j) loop call insert(a) for i=1 to 10 print a(i); next i close #12 end sub private sub insert(a() as integer) dim i as integer,putp as integer,j as integer dim getp as integer,n as integer,tem as integer n=ubound(a)/2 putp=1 getp=n+1 for i=1 to n tem=a(getp) for j=getp to putp +1 step -1 (2) next j a(putp)=tem getp=getp+1 putp= (3) next i end sub 2) 补充代码(2001春二(8)) 下面程序得功能是将无序数组中相同得数只保留一个,其余得删除,并输出经过删除后的数组元素,删除相同数是通过将该数组元素后面的元素在数组内依次前移替换前一个元素的值实现的。数组各元素的值从文件data.txt中读取。 option explict option base 1 private sub form_click() dim i as integer,j as integer,k as integer dim a()as integer,t as integer,m as integer open "c:\my documents\2000test\data.txt" for input as #1 do while (1) i=i+1 redim preserve a(i) input #1,a(i) loop m=1:t= (2) do while m do while i<=t if a(i)=a(m)then for j=1 to (3) a(j)=a(j+1) next j t=t-1 else i= (4) end if loop m=m+1 loop redim preserve a(t) for i=1 to t print a(i); next i end sub |
责任编辑:gzu521