Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 16 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
16
Dung lượng
243,67 KB
Nội dung
Dim a() As Integer Dim b() As Integer Dim n As Integer Private Sub cmdLietKeGiaTriKhacNhau_Click() Dim i As Integer b = LietKeGiaTriKacNhau(a) For i = To UBound(b) s = s & b(i) & " " Next i txtGiaTriKhacNhau.Text = s End Sub Private Sub cmdLietKeSoLanXuatHien_Click() Dim c() As Integer Dim i, j, k, m, d As Integer m = UBound(b) ReDim c(m) k = -1 For i = To m ' Dem so lan xuat hien d cua b(i) mang a d=0 For j = To n If b(i) = a(j) Then d=d+1 End If Next j ' Luu so lan xuat hien d cua b(i) mang a vao mang c k=k+1 c(k) = d Next i For i = To m s = s & c(i) & " " Next i txtTanSo.Text = s End Sub Private Sub cmdSapXep_Click() Dim i, j As Integer Dim x As Integer For j = To n ' Chen x = a(j) vao mang da sap tang a(0 To j-1) x = a(j) i=j-1 Do While i >= If a(i) > x Then a(i + 1) = a(i) i=i-1 Else Exit Do End If Loop a(i + 1) = x Next j For i = To n s = s & a(i) & " " Next i txtMangSapXep.Text = s End Sub Private Sub cmdTaoMang_Click() Dim i As Integer Dim s As String n = CInt(txtN.Text) ReDim a(n) Randomize ' Khoi tao ham sinh so ngau nhien (thay doi theo thoi gian) For i = To n a(i) = * Rnd(n) ' Sinh so ngau nhien khoang [0, 99] s = s & a(i) & " " Next i txtMang.Text = s End Sub Function LietKeGiaTriKacNhau(a() As Integer) As Integer() Dim b() As Integer Dim i, j, k, n As Integer Dim kt As Boolean n = UBound(a) ' Lay chi so cuoi cua mang a ReDim b(n) ' Cap phat mang b co n phan tu b(0) = a(0) ' Bo a(0) vao mang b j=0 ' Chi so cua phan tu moi duoc bo vao mang b For i = To n ' Kiem tra xem a(i) co mang b chua kt = False For k = To j If b(k) = a(i) Then kt = True Exit For End If Next k ' Neu a(i) co mang b thi them a(i) vao mang b If kt = False Then j=j+1 b(j) = a(i) End If Next i ' Cap phat lai mang b (giu lai cac phan tu moi bo vao va bo di cac phan tu thua) ReDim Preserve b(j) LietKeGiaTriKacNhau = b End Function Option Explicit Dim A(1 To 100) As Integer Dim N As Integer Function TimMin() As Integer Dim i As Integer Dim M As Integer M = A(1) For i = To N If A(i) < M Then M = A(i) Next TimMin = M End Function Function TimChanMax(ByRef timDuoc As Boolean) As Integer Dim i As Integer Dim M As Integer i=1 Do While i N Then MsgBox "Mang khong co so chan nao" timDuoc = False Else M = A(i) For i = i + To N If (A(i) Mod = 0) And (A(i) > M) Then M = A(i) End If Next End If timDuoc = True TimChanMax = M End Function Private Sub cmdChanMax_Click() Dim kt As Boolean, kq As Integer kq = TimChanMax(kt) If kt = True Then MsgBox kq End Sub Private Sub cmdMin_Click() MsgBox "Min = " & TimMin() End Sub Private Sub cmdNhap_Click() N = Val(InputBox("Nhap so phan tu cua mang(