ĐÁP ÁN GỢI Ý
Câu 1. (1.5 điểm)
Function KTPTTrung(b() As Integer) As Boolean
Dim i As Integer, j As Integer
KTPTTrung = False
i = 0
Do
j = i + 1
Do While j <= n
If b(i) = b(j) Then
KTPTTrung = True
Exit Function
End If
j = j + 1
Loop
i = i + 1
Loop Until i > n
End Function
Câu 2 (2.5 điểm)
a (1 điểm). Chạy tay lưu đồ với n = 12. Bảng giá trị :
i
|
j
|
(i Mod j<>0)And(j<i)
|
S
|
2
|
2
|
False
|
2,
|
3
|
2
|
True
|
2,
|
3
|
3
|
False
|
2,3,
|
4
|
2
|
False
|
2,3,
|
5
|
-
|
-
|
2,3,
|
6
|
2
|
False
|
2,3,
|
b (1.5 điểm). Viết chương trình theo lưu đồ đã cho.
Private Sub tinh_Click()
Dim n As Integer, s As String, i As Integer, j As Integer
Do
n = InputBox("Nhap n:")
Loop Until n > 3
s = ""
For i = 2 To n \ 2
If n Mod i = 0 Then
j = 2
Do While (j < i) And (i Mod j <> 0)
j = j + 1
Loop
If j = i Then s = s & i & ","
End If
Next i
MsgBox s
End Sub
Câu 3 (2 điểm):
Public Function TIMVITRICHAN (Byval a As Variant, Byval n As Integer) As String
Dim i As Long
For i = 1 To n
If a(i) Mod 2 = 0 Then
TIMVITRICHAN= str(i)
Exit Function
End If
Next i
TIMVITRICHAN = "Khong co"
End Sub
Câu 4 (2 điểm)
Public Function KTMAXCHIA3(ByVal n As Long) As Boolean
Dim dv As Long, i As Long, max As Long
‘Kiểm tra n (0.5 đ)
If n < 0 Then
MsgBox "Nhap n lon hon 0"
Exit Function
KTMAXCHIA3 = false
End If
'Tim chu so lon nhat (0.75 điểm)
max = n Mod 10
dv = 0
Do While n<> 0
dv =n Mod 10
If dv >= max Then
max = dv
End If
n = n \ 10
Loop
'Kiem tra tích co phai la so chia hết cho 3? (0.75 đ)
If max mod 3=0 Then
KTMAXCHIA3= true
Else
KTMAXCHIA3 =false
End If
End Function
Câu 5 (2 đ): có thể làm theo cách khác
Public Function DAONGUOCCHUOI (Byval s As String) As String
Dim i As Long, s2 As String, s1 As String, s3 As String
d = Len(s)
i = InStr(s, Space(1))
Do While (i < d) And (i <> 1)
s1 = Left(s, i - 1)
For i = 1 To Len(s1) ‘ nếu thiếu -0.5 đ
If Mid(s1, i, 1) >= "0" And Mid(s1, i, 1) <= "9" Then
s2 = Mid(s1, i, 1) + s2
End If
Next i
s3 = s3 + s2 ‘ nếu thiếu -0.25
s2 = " " ‘nếu thiếu -0.25 đ
s = LTrim(Right(s, d - i)) + Space(1) ‘nếu thiếu -0.25đ
d = Len(s) + 1
i = InStr(s, Space(1))
Loop
DAONGUOCCHUOI = s3 ‘ nếu thiếu -0.25 đ
End Function