试题

试题 试卷

logo

题型:综合题 题类:常考题 难易度:普通

浙江省高中信息技术 算法及其程序实现单元检测

活动课上,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

 

  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

IfThen

a(j)=a(j)+a(j-1) : a(j-1)=a(j)-a(j-1) :

  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

 

 Else

  st=st+1

 End If

Loop

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

End Sub

举一反三
小月设计了一个VB程序,用于对全班55名同学的体质调查分析,该程序能将55名同学的身高与体重录入到数据库中,之后通过如图所示的VB应用程序计算出这些同学的BMI指数,并按BMI指数从小到大进行排序,并显示这些学生的体质情况。

    注:BMI指数的计算公式为:BMI=体重(千克)除以身高(米)的平方(kg/m2)。 BMI<18.5为偏瘦(表明营养不足);BMI=18.5~24.9为体重正常;BMI=25~29.9为超重;BMI>30为肥胖。

该VB应用程序的代码如下:

Dim stuBMI(1 To 55) As Single

Dim stuName(1 To 55) As String

Private Sub Command1_Click()

    Dim k As Integer, i As Integer, j As Integer

    Dim t1 As Single, t2 As String

    Dim Results As String

    For i = 1 To 54        

        For j = i + 1 To 55

            If stuBMI(j) < stuBMI(k) then      ②    

        Next j

        If  k<>I  Then  

            t1 = stuBMI(i): stuBMI(i) = stuBMI(k): stuBMI(k) = t1

            t2 = stuName(i): stuName(i) = stuName(k): stuName(k) = t2

        End If

    Next i

List1.AddItem "姓名" + vbTab + "BMI指数" + vbTab + "结果"  

    For i = 1 To 55

        If stuBMI(i) < 18.5 Then

            Results = "偏瘦"

        ElseIf stuBMI(i) >= 18.5 And stuBMI(i) < 25 Then

            Results = "正常"

        ElseIf stuBMI(i) >= 25 And stuBMI(i) < 30 Then

            Results = "超重"

        Else

            Results = "肥胖"

        End If

        List1.AddItem stuName(i) + vbTab + str(stuBMI(i)) + vbTab + Results

    Next i

End Sub

Private Sub Form_load()

    Dim conn As New ADODB.Connection

    Dim rs As New ADODB.Recordset

    Dim strSQL As String

    Dim i As Integer

    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + App.Path + "\Student.accdb"

    conn.Open

    strSQL = "SELECT * FROM Student"

    Set rs.ActiveConnection = conn

    rs.Open strSQL

    i = 0

    Do While Not rs.EOF

        i = i + 1

        stuName(i) = rs.Fields("StuName")

        stuBMI(i) = Round(rs.Fields("Weight") / rs.Fields("height") ^ 2, 1)

        rs.MoveNext

    Loop

    rs.Close

    conn.Close

    Set rs = Nothing

    Set conn = Nothing

End Sub

返回首页

试题篮