bài tập tin Đại cươngkada

48
Page 1 BÀI TẬP TIN ĐẠI CƯƠNG Bài tập chỉ mang tính tham khảo và chưa hoàn thiện mong các bạn hoàn thiện thêm Lê Đức Thông

Upload: tutjtit

Post on 18-Dec-2015

214 views

Category:

Documents


0 download

DESCRIPTION

dâd

TRANSCRIPT

BI TP TIN I CNG

BI TP TIN I CNG

BI TP TIN I CNG

Bi tp ch mang tnh tham kho v cha hon thin mong cc bn hon thin thm

L c Thng

CHNG VBi 5.5Private Sub Command1_Click() Dim n%, s& Do n = InputBox("nhap so : ") Loop Until (n >= 0) s = 1 For i = 1 To n s = s * i Next i MsgBox (n & " ! " & " = " & s)End SubBi 5.6Private Sub Command1_Click() Dim n%, s1&, s2&, s!, i% Do n = InputBox("nhap so") Loop Until (n > 0) s1 = 0: s2 = 0 For i = 1 To n s1 = s1 + i ^ 2 s2 = s2 + i * (i + 1) Next i s = s1 / s2 MsgBox ("Tn = " & s)End SubBi 5.9Private Sub Command1_Click() Dim a%, b%, c%, max% Do a = InputBox("nhap so thu 1 = ") b = InputBox("nhap so thu 2 = ") c = InputBox("nhap so thu 3 = ") Loop Until (a > 0) And (b > 0) And (c > 0) max = a If a > b Then If a < c Then max = c Else If b > c Then max = b Else max = c End If End If MsgBox ("so lon nhat = " & max)End SubBi 5.10Private Sub Command1_Click() Dim a$, s$ a = InputBox("nhap 1 ky tu ") If (Asc(a) >= 48) And (Asc(a) = 65) And (Asc(a) = 97) And (Asc(a) 0) s1 = 0 For i = 1 To n s1 = s1 + i Next i s2 = (n * (n + 1)) / 2 Print Str(s1) & " " & Str(s2) If (s1 = s2) Then Print "cong thuc dung"End SubBi 5.12Private Sub Command1_Click() Dim n%, s1&, s2&, i% Do n = InputBox("nhap so = ") Loop Until (n > 0) s1 = 0 For i = 1 To n s1 = s1 + i ^ 2 Next i s2 = (n * (n + 1) * (2 * n + 1)) / 6 Print s1 & " " & s2 If (s1 = s2) Then Print "cong thuc dung"End SubBi 5.13Function gt(n%) As Long Dim i% gt = 1 For i = 1 To n gt = gt * i Next iEnd Function

Private Sub Command1_Click() Dim n%, m%, s!, s1&, i% Do n = InputBox("nhap n") m = InputBox("nhap m") Loop Until (m >= 0) And (n > m) s1 = 1 For i = 1 To m - 1 s1 = s1 * (n - i) Next i s1 = n * s1 s = s1 / gt(m) Print "to hop chap " & m & " cua " & n & " la = " & sEnd SubBi 5.14Private Sub Command1_Click() Dim chuc%, dv%, i%, s&, kq1$, kq2$, kq3$ kq1 = "": kq2 = "": kq3 = "" For i = 10 To 99 chuc = i \ 10 dv = i Mod 10 s = s + i If (chuc >= 3) And (chuc = 2) And (dv 0) And (c < 1) k = 1 i = 1 e = 1 Do While (k > c) k = 1 / gt(i) e = e + k i = i + 1 Loop Print eEnd SubBi 5.16Private Sub Command1_Click() Dim a#, i&, s# a = InputBox("nhap so") i = 1 s = 0 Do While (s < a) s = s + 1 / i i = i + 1 Loop Print iEnd SubBi 5.17Private Sub Command1_Click() Dim i% For i = 1 To 100 s1 = i ^ (1 / 2) s2 = i ^ (1 / 3) s3 = i ^ (1 / 4) Print s1 & " " & s2 & " " & s3 Next iEnd SubBi 5.18Private Sub Command1_Click() Dim a&, x$, n%, dem% a = InputBox("nhap so") x = Cstr(a) n = Len(x) dem = 0 For i = 1 To n If Val(Mid(x, i, 1)) Mod 2 0 Then dem = dem + 1 Next i Print "so cac so le = " & demEnd SubBi 5.19Function kthh(n%) As Boolean Dim i%, s& kthh = True s = 0 For i = 1 To n - 1 If (n Mod i = 0) Then s = s + i End If Next i If (s n) Then kthh = FalseEnd Function

Private Sub Command1_Click() Dim n%, i%, j%, kp$ n = InputBox("nhap so") kq = "" For i = 1 To n If kthh(i) = True Then kq = kq & Str(i) & " " Next i Print kqEnd SubBi 5.20Private Sub Command1_Click() Dim s&, a% s = 0 Do a = InputBox("nhap so") s = s + a Loop While (a 0) Print sEnd SubBi 5.21Private Sub Command1_Click() Dim a%, n% Do a = InputBox("nhap so") Loop Until a > 0 n = Len(CStr(a)) Print nEnd SubBi 5.23Function gt(n%) As Long Dim i% gt = 1 For i = 1 To n gt = gt * i Next iEnd Function

Private Sub Command1_Click() Dim x!, n%, e!, s!, i% Do e = InputBox("nhap sai so") Loop Until (e > 0) And (e < 1) x = InputBox("nhap x") k = 1 s = 0 i = 1 Do While (Abs(k) > e) s = s + k k = x ^ i / gt(i) i = i + 1 Loop Print ("gia tri e^" & x & " la = " & s)End SubBi 5.25Function gt(n%) As Double Dim i%, s gt = 1 For i = 1 To n gt = gt * i Next iEnd Function

Private Sub Command1_Click() Dim x!, i%, cos!, k! x = InputBox("nhap goc") k = 1: i = 1 cos = 0 Do While (Abs(k) > 0.001) cos = cos + k k = ((-1) ^ i * x ^ (2 * i)) / gt(2 * i) i = i + 1 Loop Print "gia tri cua cos(" & x & ") = " & cosEnd SubBai 5.26

Private Sub command1_click() Dim pi!, i#, k# pi = 0: k = 1: i = 1 Do While (Abs(k) > 0.00001) pi = pi + k k = (-1) ^ i / (2 * i + 1) i = i + 1 Loop pi = pi * 4 Print "gia tri pi = " & piEnd SubBai 5.28Function nt(n%) As Boolean Dim i% i = 2 nt = True Do While (i < n) And (n Mod i 0) i = i + 1 Loop If i n Then nt = FalseEnd Function

Private Sub command1_click() Dim i%, kq$ kq = "" For i = 1 To 99 If nt(i) = True Then kq = kq & CStr(i) & " " Next i Print "cac so nt < 100 la : " & kqEnd SubBi 5.29Function ktl(n%) As Boolean ktl = True If n Mod 2 = 0 Then ktl = FalseEnd Function

Private Sub command1_click() Dim i1%, i2%, s!, i% Do i1 = InputBox("nhap so thu nhat") i2 = InputBox("nhap so thu hai") Loop Until (i1 > 0) And (i1 < i2) s = 0 For i = i1 To i2 If ktl(i) = True Then s = s + i Next i Print "ket qua can tim : " & sEnd SubBi 5.36Private Sub Command1_Click() Dim x$, n%, i%, j% x = InputBox("nhap xau") n = Len(x) t = 0 For i = 1 To n dem = 1 For j = i + 1 To n If (Asc(Mid(x, i, 1)) 32) And (Mid(x, i, 1) = Mid(x, j, 1)) Then dem = dem + 1 End If Next j If (Asc(Mid(x, i, 1)) 32) Then MsgBox ("co " & dem & " chu " & Mid(x, i, 1)) t = t + dem End If x = Replace(x, Mid(x, i, 1), " ") Next i MsgBox ("so ki tu trang : " & n - t)End SubBi 5.37Private Sub Command1_Click() Dim st$, st1$, stg$, k%, i%, j% Dim x() As Integer st = InputBox("nhap chuoi st") st1 = InputBox("nhap chuoi st1") n = Len(st): k = 0: j = Len(st1) ReDim x(n) For i = 1 To n stg = Mid(st, i, j) If (stg = st1) Then k = k + 1 x(k) = i End If Next i st = "" For i = 1 To k st = st & x(i) & " " Next i Print ("chuoi '" & st1 & "' xuat hien " & k & " lan tai cac vi tri " & st)End SubBi 5.38Private Sub Command1_Click() Dim x() As String Dim i%, sv$, tam$, n% Do n = InputBox("nhap so sinh vien") Loop Until (n > 0) ReDim x(n) For i = 1 To n x(i) = InputBox("sinh vien thu " & i) x(i) = UCase(Trim(x(i))) Next i j = 1 Do While (j x(i + 1) Then tam = x(i) x(i) = x(i + 1) x(i + 1) = tam End If Next i j = j + 1 Loop sv = "" For i = 1 To n sv = sv & x(i) & ", " Next i MsgBox (sv)End SubBi 5.39Private Sub Command1_Click() Dim xau1$, xau2$, n%, i%, m%, j%, kq$ xau1 = InputBox("nhap xau 1") xau2 = InputBox("nhap xau 2") n = Len(Trim(xau1)) m = Len(Trim(xau2)) kq = "" For i = 1 To n For j = 1 To m If (Mid(xau1, i, 1) = Mid(xau2, j, 1)) And (Asc(Mid(xau1, i, 1)) 32) Then kq = kq & Mid(xau1, i, 1) & " " xau2 = Replace(xau2, Mid(xau2, j, 1), " ") End If Next j Next i Print "cac ky tu xuat hien trong ca 2 xau : " & kqEnd SubBi 5.40Private Sub Command1_Click() Dim x$, x1$, n%, i%, kq$, j%, n1% x = InputBox("nhap chuoi ky tu") x = Trim(x) x = x & " " n = Len(x) i = 1 kq = "" Do While (i < n) vt = InStr(i, x, " ") x1 = Mid(x, i, vt - i) n1 = Len(Trim(x1)) For j = 1 To n1 If Mid(x1, j, 1) = "h" Then kq = kq & x1 & " " Exit For End If Next j i = vt i = i + 1 MsgBox (x1) Loop Print kqEnd SubCHNG VIBi 6.1Function demduong(x() As Single, n%) As Long Dim i% demduong = 0 For i = 1 To n If (x(i) > 0) Then demduong = demduong + 1 Next iEnd FunctionFunction demam(x() As Single, n%) As Long Dim i% demam = 0 For i = 1 To n If (x(i) < 0) Then demam = demam + 1 Next iEnd Function

Private Sub command1_click() Dim x() As Single, n%, i% Do n = InputBox("nhap so phan tu mang") Loop Until (n > 0) ReDim x(n) For i = 1 To n x(i) = InputBox("x[" & i & "] = ") Next i Print ("so luong so duong la : " & demduong(x(), n)) Print ("so luong so am la : " & demam(x(), n)) Print ("so luong so 0 la : " & n - demduong(x(), n) - demam(x(), n))End SubBai 6.2Function sochan(x() As Integer, n%) As String Dim i% sochan = "" For i = 1 To n If (x(i) Mod 2 = 0) Then sochan = sochan & CStr(x(i)) & " " End If Next iEnd Function

Function sole(x() As Integer, n%) As String Dim i% sole = "" For i = 1 To n If (x(i) Mod 2 0) Then sole = sole & CStr(x(i)) & " " End If Next iEnd Function

Private Sub Command1_Click() Dim x() As Integer, n%, i% Do n = InputBox("nhap so phan tu mang ") Loop Until (n > 0) ReDim x(n) For i = 1 To n x(i) = InputBox("x[" & i & "] = ") Next i Print sochan(x(), n) Print sole(x(), n)End SubBi 6.3

Function fibonaci(n%) As String Dim i%, x() As Integer ReDim x(n) x(1) = 1 x(2) = 1 For i = 3 To n x(i) = x(i - 1) + x(i - 2) Next i fibonaci = "" For i = 1 To n fibonaci = fibonaci & CStr(x(i)) & " " Next iEnd Function

Private Sub Command1_Click() Dim n% Do n = InputBox("so phan tu mang") Loop Until (n >= 0) And (n = 0) And (n 0) And (n < 100) ReDim x(n) For i = 1 To n x(i) = InputBox("x[" & i & "] = ") Next i If ktdoixung(x(), n) = True Then Print ("day doi xung") Else Print ("day khong doi xung") End If If ktcsc(x(), n) = True Then Print "day la cap so cong voi cong sai : " & x(2) - x(1) Else Print "day khong phai cap so cong" End IfEnd SubBi 6.9Function ktnt(n%) As Boolean Dim i% ktnt = True i = 2 Do While (i < n) And (n Mod i 0) i = i + 1 Loop If i n Then ktnt = FalseEnd Function

Function kttm(n%) As Boolean Dim i%, st%, st1% kttm = False For i = 2 To n st = i: st1 = n - i If (ktnt(st) = True) And (ktnt(st1) = True) Then kttm = True End If Next iEnd Function

Private Sub Command1_Click() Dim x() As Integer, i%, n%, kq$ Do n = InputBox("nhap so phan tu mang") Loop Until (n > 0) And (n n Then ktcp = FalseEnd Function

Sub sxntldd(x() As Integer, n%) Dim i%, m%, tam% m = 1 Do While (m 1) Then tam = x(i - 1) x(i - 1) = x(i) x(i) = tam End If Next i m = m + 1 LoopEnd Sub

Sub sxcpxcd(x() As Integer, n%) Dim i%, tam%, j% m = 1 Do While (m 0) And (n n then ktsohoanhao = falseend functionIII. TM S CHNH PHNG: s chnh phng l s bng bnh phng ca mt s nguynCho mt s n bt k kim tra xem n c phi s chnh phng khng ta thc hin nh sau:

Function ktchinhphuong(n%) as BooleanDim i%Ktchinhphuong = trueFor i=1 to nIf i^2 = n then exit forNext iIf i>n then ktchinhphuong = falseEnd functionIV. KIM TRA XEM DY S C I XNG HAY KHNG kim tra xem mt dy s c i xng hay khng ta lm nh sau:

Function ktdoixung(x() as interger,n%) as BooleanDim i%Ktdoixung = trueFor i=1 to n\2If x(i) < > x(n-i+1) then Ktdoixung = falseExit forEnd ifNext iEnd function

V. O NGC DY S: o ngc mt dy s ta lm nh sau:

Sub daonguocday(x() as single, n%) Dim i%, tam!For i=1 to n\2tam=x(i)x(i) = x(n i +1)x(n i +1) = tamnext iend sub

VI. CHUYN PHN T V U CHUI:Mun chuyn mt phn t v u chui ta to ra mt bin tm c cng kiu d liu vi phn t chui v thc hin nh sau:

Tam = x(i)x(i) = x(i+1)x(i+1) = tam

VII. CHUYN PHN T XUNG CUI CHUI:Mun chuyn mt phn t xung cui chui ta cng to ra mt bin tam v thc hin nh sau:

Tam = x(i+1)x(i+1) = x(i)x(i) = tam

VIII. LY T RA T CHUI: ly mt k t ra t chui ta lm nh sau:Gi s ta c mt chui k t x bt k

x=trim(x) & n=len(x)i=1do while (i= 0) And (n n Then ktcp = FalseEnd Function

Private Sub Command1_Click() Dim n% Do n = InputBox("nhap n") Loop Until (n >= 0) If (ktcp(n) = True) And (n Mod 2 = 0) Then Print n & " thoa man dieu kien dau bai" Else Print n & " khong thoa man dieu kien dau bai" End IfEnd SubBi 2Private Type canbo hoten As String ngaysinh As Date donvi As StringEnd Type

Function ho(x$) As String Dim vt% vt = InStr(1, x, " ") ho = Mid(x, 1, vt - 1)End Function

Private Sub Command1_Click() Dim cb() As canbo, i%, m%, n%, fn% n = InputBox("nhap so can bo") ReDim cb(n) For i = 1 To n cb(i).hoten = InputBox("nhap ho ten can bo thu " & i) cb(i).ngaysinh = InputBox("nhap ngay sinh can bo thu " & i) cb(i).donvi = InputBox("nhap don vi can bo thu " & i) Next fn = FreeFile Open "d:\danhsach.txt" For Output As #fn For i = 1 To n If ho(cb(i).hoten) = "tran" Then Print #fn, cb(i).hoten Print #fn, cb(i).ngaysinh Print #fn, cb(i).donvi End If Next Close #fnEnd Sub 2:1. Xy dng chng trnh nhp vo h tn ca mt ngi. sau in ra su h tn vi k t u tin ca cc t c i thnh ch hoa, cc k t cn li ch thng.Vd: h tn nhp vo l trAn vAN hUnG xu sau khi in s l Tran Van Hung.2. Xy dng chng trnh cho php nhp vo ta (x,y) ca 2 im, tnh v kim tra xem im c i xng nhau qua gc ta hay khng?Bi lmBi 1.Function chuanhoa(x$) As String Dim vt%, i%, x1 x1 = LCase(Trim(x)) vt = 1: i = 1 Do While (vt 0) vt = InStr(i, x1, " ") x1 = Mid(x1, 1, vt) & UCase(Mid(x1, vt + 1, 1)) & Mid(x1, vt + 2) i = vt + 1 Loop chuanhoa = x1End Function

Private Sub Command1_Click() Dim x$ x = InputBox("nhap chuoi") Print chuanhoa(x)End SubBi 2.Private Sub Command1_Click() Dim x1%, x2%, y1%, y2% x1 = InputBox("nhap hoanh do diem 1") y1 = InputBox("nhap tung do diem 1") x2 = InputBox("nhap hoanh do diem 2") y2 = InputBox("nhap tung do diem 2") If (x1 = -x2) And (y1 = -y2) Then Print ("hai diem doi xung nhau qua goc toa do") Else Print ("hai diem khong doi xung nhau qua goc toa do") End IfEnd Sub

3:1. Xy dng chng trnh tm v in ra tt c cc s t nhin c 4 ch s m trong m s khng c 2 ch s no ging nhau. V d 3742 khng c 2 ch s ging nhau.2. Xy dng chng trnh cho php nhp vo 2 phn s (bao gm t s v mu s) . hy tin ra tng 2 phn s dng ti gin.Bi lmBi 1.Private Sub Command1_Click() Dim i%, n% For i = 1234 To 6789 x = CStr(i) n = Len(x) For j = 1 To n For k = j + 1 To n If Val(Mid(x, j, 1)) = Val(Mid(x, k, 1)) Then Exit For Next k If k n Then Print i Next iEnd SubBi 2.Private Sub Command1_Click() Dim a1%, a2%, b1%, b2%, x1$, x2$, vt1%, vt2% Do x1 = InputBox("nhap phan so thu 1") x2 = InputBox("nhap phan so thu 2") vt1 = InStr(1, x1, "/") vt2 = InStr(1, x2, "/") Loop Until (vt1 0) And (vt2 0) a1 = Val(Mid(x1, 1, vt1 - 1)) a2 = Val(Mid(x1, vt1 + 1)) b1 = Val(Mid(x2, 1, vt2 - 1)) b2 = Val(Mid(x2, vt2 + 1)) a1 = a1 * b2 + b1 * a2 a2 = a2 * b2 For b1 = a1 To 1 Step -1 If (a1 Mod b1 = 0) And (a2 Mod b1 = 0) Then Exit For Next b1 a1 = a1 / b1 a2 = a2 / b1 Print CStr(a1) & "/" & CStr(a2)End Sub 4:1. Vit chng trnh nhp vo mt s thc a (a#0) v mt s nguyn dng n sau tnh :S = 2. Nhp vo 2 s nguyn dng a v b, xt xem tng cc ch s ca (a+b) c phi l s chn hay khng?Bi lmBi 1.Private Sub Command1_Click() Dim n%, a!, i%, s! Do n = InputBox("nhap so n") Loop Until n >= 0 a = InputBox("nhap so thuc a") s = 0 For i = n To 1 Step -1 s = Sqr(s + Abs(a) ^ -i) Next i Print sEnd SubBi 2.

5: 1. Vit chng trnh nhp vo mt dy s nguyn dng x1, , xn (n 0) And (n < 100) ReDim x(n) For i = 1 To n x(i) = InputBox("x[" & i & "] = ") Next i dem = 0 For i = 1 To n If (x(i) Mod 13 = 0) And (x(i) Mod 2 = 0) Then dem = dem + 1 Print x(i) End If Next i If dem = 0 Then Print "khong co phan tu nao thoa man"End SubBi 2

: 61. Vit chng trnh nhp vo 1 s nguyn dng n, sau tnh biu thc sau:S = , trong j!! = 2. Vit chng trnh nhp vo mt s nguyn dng n, sau chn ch s nh nht ca n vo sau ch s chn ca n.Bi lmBi 1.Function gtt(n%) As Single Dim i% gtt = 1 For i = 1 To n If n Mod 2 = 0 Then If i Mod 2 = 0 Then gtt = gtt * i End If Else If i Mod 2 0 Then gtt = gtt * i End If End If Next iEnd Function

Private Sub Command1_Click() Dim n%, i%, j%, s!, s1! Do n = InputBox("nhap n = ") Loop Until n >= 0 s = 0 For i = 1 To n s1 = 0 For j = 1 To i s1 = s1 + 1 / gtt(j) Next j s = s + Log(i) * s1 Next i Print "tong can tim = " & sEnd SubBi 2.Function min(a%) As Integer Dim x$, i%, n% x = Trim(a) n = Len(x) min = Val(Mid(x, 1, 1)) For i = 1 To n If Val(Mid(x, i, 1)) < min Then min = Val(Mid(x, i, 1)) End If Next iEnd Function

Function demchan(a%) As Integer Dim i%, n%, x$ x = CStr(a) demchan = 0 n = Len(x) For i = 1 To n If Val(Mid(x, i, 1)) Mod 2 = 0 Then demchan = demchan + 1 End If Next iEnd Function

Private Sub Command1_Click() Dim n%, x$, i%, m% Do n = InputBox("nhap n = ") Loop Until n >= 0 x = Trim(n) m = Len(x) + demchan(n) - 1 Print demchan(n) For i = 1 To m If Val(Mid(x, i, 1)) Mod 2 = 0 Then x = Mid(x, 1, i) & CStr(min(n)) & Mid(x, i + 1) i = i + 1 End If Next i Print xEnd Sub 7:1. Xy dng chng trnh nhp s liu t tp dayso.dat vo 1 dy s nguyn x1,x2, . . . ,xn gm n phn t. hy in ra v tr phn t m u tin trong dy. nu khng c phn t no th in ra thng bo.2. Xy dng chng trnh cho php nhp vo 1 s nguyn dng n tach n thanh 2 so nguyn dng a,b (n = a+b), sao cho tch p= a*b2 t cc i.Bi lmBi 1.Private Sub Command1_Click() Dim n%, i%, fn%, x() As Integer fn = FreeFile Open "DATA.DAT" For Input As #fn Input #fn, n ReDim x(n) For i = 1 To n Input #fn, x(i) Next i Close #fn For i = 1 To n If x(i) < 0 Then Exit For Next i If i max Then max = p a1 = a b1 = b End If Next Print "a = " & a1 Print "b = " & b1End SubPage 1

L c Thngtrang 2