btcq.net
当前位置:首页 >> 编写vB程序:对已知存放在数组中的6个数由从小到大排序 >>

编写vB程序:对已知存放在数组中的6个数由从小到大排序

Private Sub Command1_Click() Dim a(1 To 6) Cls Print "排序前的数为:"; For i = 1 To 6 a(i) = Int(Rnd * 90 + 10) Print a(i); Next i Print '排序 For i = 1 To 5 For j = i + 1 To 6 If a(i) > a(j) Then b = a(i): a(i) = a(j): a(j) = b Next j Next i Print "排序后的数为:"; For i = 1 To 6 Print a(i); Next i End Sub

1、冒泡法(递增) 算法思想:(将相邻两个数比较,大的调到后头) 1)有n+1个数(存放在数组a(n)中),第一趟将每相邻两个数比较,大的调到后头,经n次两两相邻比较后,最大的数已“沉底”,放在最后一个位置,小数上升“浮起”

'在窗体中添加一个button,一个textboxOption ExplicitPrivate Sub Command1_Click()Dim intNum(5) As Integer, intTemp As IntegerDim i As Integer, j As IntegerDim strTemp As String'随机生成6位数字Randomize TimerText1 = Int(Rnd * 1000000)'

private sub form_click()dim a(1 to 10) as integerfor i = 1 to 10a(i) = inputbox("输入")next ifor i = 1 to 10p = ifor j = i + 1 to 10if a(p) 评论0 0 0

Private Sub Command1_Click()Text2 = StrReverse(Text1)End Sub

c语言:void main() { int a[]={3,2,5,8,1,0,7,4,6,9}; int i,j,k;//冒泡法排序 for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if(a[i]>a[j]) { k=a[i]; a[i]=a[j]; a[j]=a[i]; } } }//输出 for(i=0;i<10;i++) { printf("%d\n",a[i]); } }

private sub command1_click() dim i, j as integer dim temp as integer dim a a = split(trim(text1.text), ",") for i = 0 to ubound(a)-1 for j = i + 1 to ubound(a) if int(a(i)) > int(a(j)) then temp = a(j) a(j) = a(i) a(i) = temp end if next text2.text = text2.text & a(i)

6个text控件中的数字放到一个数组里面,对这个数组排序

方法/步骤 打开VB6.0,新建一个工程,在窗体中添加两个命令按钮,caption分别改为“升序排列”和“降序排列”,添加两个个标签控件,将名字分别改为Lbl1和Lbl2,将窗体Form1的caption属性改为“选择法数组排序”,将界面设置成下图

Private Sub Form_Click() Dim a%(9), i%, j%, t% Randomize For i = 0 To UBound(a) a(i) = Int(Rnd * 101) + 100 Next For i = 0 To UBound(a) - 1 For j = 0 To UBound(a) - i - 1 If a(j) > a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = t End If Next j, i Me.Cls For i = 0 To UBound(a) Print a(i); NextEnd Sub

相关文档
网站首页 | 网站地图
All rights reserved Powered by www.btcq.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com