试题

试题 试卷

logo

题型:单选题 题类:常考题 难易度:普通

浙江省高中信息技术 排序算法及程序实现同步练习

某算法的部分程序段如下:

For i=1 To 7

 k=i

 For j=i+1 To 8

  If a(j)>a(k) And a(j)>85 Then k=j

 Next j

 If k<>i Then t=a(i): a(i)=a(k): a(k)=t

Next i

数组元素a(1)到a(8)的原始数据依次为“89,70,79,85,99,80,82,74”,则第3遍“加工”后数组元素a(1)到a(8)的数据依次是(  )

A、99,89,79,85,80,70,82,74 B、99,89,85,79,70,80,82,74 C、99,89,79,85,70,80,82,74 D、99,89,85,82,80,79,74,70
举一反三
活动课上,n个学生要两两组队进行拔河比赛,要求每个小组总体重不超过120kg,小林想知道最多可以组成多少个队伍,并希望得到可行的组队方案。于是设计了如下界面的程序,在文本框Text1中输入n 个学生的体重(数字之间用逗号隔开),单击“队伍”按钮Command1后在标签Label1上显示最多可组队数量,同时在列表框List1输出方案。

实现上述功能程序如下,在横线处填入合适的代码。

Dim n As Integer

Dim a(1 To 50) As Integer

Sub makedata(s As String)     ‘该过程实现将输入的体重分别存入数组a中

Dim in As Long, x As Long, c As String, i As Integer

m=Len (s) : n=0

For i=1 To m

 c=Mid(s, i, 1)

 If c >=“0” And c <=“9” Then

  {#blank#}1{#/blank#}

  x=x+Asc(c)-Asc(“0”)

 Else

  If x > 0 Then n=n+1: a(n)=x

  x=0

 End If

Next i

n=n+1: a(n)=x

End Sub

Private Sub Command1_Click()

Dim s As String, i As Integer, j As Integer, t As Integer

Dim cnt As Integer, st As Integer, ed As Integer

s=Text1.Text Call makedata(s)      ‘调用过程

For i=1 To n-1     ‘实现降序排序

For j=n To i+1 Step-1

If{#blank#}2{#/blank#}Then

a(j)=a(j)+a(j-1) : a(j-1)=a(j)-a(j-1) :{#blank#}3{#/blank#}

  End If

 Next j

Next i

‘下列程序段实现计算最多可组队伍

cnt=0: st=1: ed=n

Do While st < ed

 If a(st)+a(ecl) <=120 Then

  List1. AddItem Str(a(st))+“和”+Str(a(ed))+“组队”

  cnt=cnt+1

  st=st+1

  {#blank#}4{#/blank#}

 Else

  st=st+1

 End If

Loop

Label2. Caption=“最多可以组”+Str (cnt)+“组队伍”

End Sub

返回首页

试题篮