题库 高中信息

题干

有一组正整数,要求供对其中的素数进行升序排序。排序后素数在前,非素数在后。排序示例如下。
排序前
86
71
5
41
81
79
37
89
排序后
5
37
41
71
79
89
86
81
 
Const n = 8
Dim a(1 To n) As Integer
Private Sub Command1_Click()

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

Dim flag As Boolean

'读取一组正整数,存储在数组a中,代码略

For i = 1 To n – 1

 _____________

   If IsPrime(a(k)) Then flag = True Else flag = False

For j = i + 1 To n
If IsPime(a(j)) Then
If   Then ______________
k = j
flag = True
End If

End If

Next j

If k <> i Then

t = a(k): a(k) = a(i): a(i) = t

End If

If Not flag Then Exit For  'Exit For表示退出循环

Next i

'依次输出排序后的数据。代码略

End Sub
Function IsPrime(m As Integer) As Boolean
'本函数判断m是否是素数:是素数返回值为True,不是素数返回值为False
'代码略
End Function
上一题 下一题 0.99难度 填空题 更新时间:2019-11-27 12:10:06

答案(点此获取答案解析)