自考能考研究生吗:关于VB6.0算法的问题

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 02:34:06
我想在大约60个变量(数值型)中找出最大的,最小的各10个,应该怎么设计算法?
如果能附上代码就更好了.
希望写出代码,有代码的优先考虑!

排序,冒泡,插入,什么方法都可以
取前10个和后10个
回答者:该隐_堕 - 助理 二级 1-21 18:18
这是对的
这个问题其实相当简单的,建议楼主看看一些基本算法

'init,将60个变量传递到一个数组里面
'这里使用的事intTmp(1 to 60)

'下面是求解的算法
dim intTmp(1 to 60) as integer
dim sub main()
dim i as integer
dim j as integer
dim x as integer
dim y as integer
'依次排序,在数组中序号最大的就是排出来做大的
for j=60 to 2 step -1
for i=1 to j-1 step 1
if intTmp(i)>intTmp(i+1) then
x=inttmp(i)
y=inttmp(i+1)
inttmp(i+1)=x
inttmp(i)=y
end if
next i
next j
'然后inttmp(1 to 10)就是最小的
'inttmp(51 to 60)就是最大的
'数据的大小和他们的序号的大小一样
outputres
end sub
'输出代码,可以在立即窗口里面看看是不是排序出来了
dim sub outputres()
dim strRes as string
dim i as integer
strres=""
for i=1 to 10
strres=cstr(inttmp(i)) & " "
next i
debug.print strres
strres=""
for i=51 to 60
strres=cstr(inttmp(i)) & " "
next i
debug.print strres
end sub

'没有调试环境,所以不知道效果如何,请自己试
dim sub init()
'这里给出一个初始化数组的算法,可以用这个试验一下上面的main到底能不能满足
dim i as integer
for i=1 to 60
inttmp(i)=cint(random(60))
'没有环境,所以不知道这里用得random函数的用法是不是正确的
next i
end sub

定义两个长度为10的数组一个用于保存大的,一个小的
循环10次
...将60个变量中剩余的都比较一次同时找出其中最大和
...最小的保存起来
...将找出的从变量中去掉
...直到找出各10个为止
结束循环

排序,冒泡,插入,什么方法都可以
取前10个和后10个