Tài liệu GIAI DE THI

4 236 0
Tài liệu GIAI DE THI

Đang tải... (xem toàn văn)

Thông tin tài liệu

GIẢI ĐỀ THI CÁC NĂM Laptrinh_Hieu 24 Bài giải chỉ mang tính chất tham khảo. Không phải là đáp án của trường. ĐỀ THI NĂM 2004 1. Câu 1 function ti(n:word):word; begin if n<10 then ti := n else ti := (n mod 10) + ti(n div 10); end; 2. Câu 2 Câu b,c xem bài 14 và đáp án phần Danh sách móc nối. {Câu a} procedure load_file; var f: text; so:word; r: tro; begin assign(f,'solieu.dat'); {$I-} reset(f); close(f); {$I+} if IOResult <> 0 then begin writeln('khong tim thay'); readln; halt; end else begin p := nil; reset(f); while not eof(f) do begin read(f,so); new(r); r^.gtri := so; r^.tiep := p; p := r; end; close(f); end; end; 3. Câu 3 {Câu a} function tim(t: tro; n: word): string; begin if t = nil then begin tim:= ''; writeln('Khong tim thay'); end else if t^.soao = n then tim := t^.ten else if (T^.soao < n) then tim := tim(T^.r, n) else tim := tim(T^.l, n); end; {Câu a} function dem(t:tro):word; begin if t = nil then dem := 0 else dem := 1+ dem(t^.l)+ dem (t^.r); end; {Câu a} procedure bosung(var t: tro; n: word; tenct: string); begin if t = nil then begin new(t); t^.soao := n; t^.ten := tenct; t^.l := nil; t^.r := nil; end else if n = t^.soao then writeln(n, ' da co') else if n > t^.soao then bosung(t^.r,n,tenct) else bosung(t^.l,n,tenct); end; 4. Câu 4 Xem bài tập phần Đồ thị GIẢI ĐỀ THI CÁC NĂM Laptrinh_Hieu 25 ĐỀ THI NĂM 2006 1. Câu 1 type tro = ^nut; nut = record gtri: byte; tiep: tro; end; mang = array[1 100] of byte; var n: word; procedure np_mang(n: word); var top,i: word; a: mang; begin top :=0; while n>0 do begin top := top+1; a[top] := n mod 2; n := n div 2; end; for i:= top downto 1 do write(a[i]); writeln; end; procedure np_ds(n: word); var l,p:tro; begin l:= nil; while n>0 do begin new(p); p^.gtri := n mod 2; p^.tiep := l; l := p; n := n div 2; end; p:=l; while p<>nil do begin write(p^.gtri); p := p^.tiep; end; writeln; end; procedure np(n: word); begin if n=1 then write(1) else begin np(n div 2); write(n mod 2); end; end; begin write('n = '); readln(n); np_mang(n); np_ds(n); np(n); readln; end. 2. Câu 2 Câu b dùng thuật toán Dijstra program de2006_2; type mang=array[1 100,1 100] of word; mang1 = array[1 100] of word; var a: mang; n: word; mien: mang1; {Đánh dấu vùng liên thông} procedure try(j,dem: word); var i:integer; begin mien[j] := dem; for i:=1 to n do if (mien[i] = 0) and (a[j,i]>0) then begin try(i,dem); end; end; {Tìm đỉnh đầu tiên của miền thứ m} function tim_dinh(m: word): word; var i: word; begin for i:=1 to n do if mien[i] = m then begin tim_dinh := i; exit; end; tim_dinh := 0; end; procedure bosung; var i,j,dem: word; begin {xac dinh mien lien thong} fillchar(mien,100,0); dem := 0; for i:=1 to n do if mien[i] = 0 then begin dem := dem+1; try(i,dem); end; GIẢI ĐỀ THI CÁC NĂM Laptrinh_Hieu 26 if dem >1 then begin for i:=2 to dem do begin j := tim_dinh(i); a[1,j] := 1; a[j,1] := 1; {M[1] = 1 ??} end; end; end; 3. Câu 3 program de2006_3; type tros = ^sach; sach = record tens: string; namxb:word; tiep: tros; end; trotg = ^tacgia; tacgia = record matg: word; tentg: string; sachtg: tros; trai,phai: trotg; end; var T : trotg; oten: string; osach: tros; procedure xuat; {Hiện kết quả tìm kiếm} var p: tros; begin write(oten); p := osach; while p<> nil do begin write('ten sach: ',p^.tens); writeln('nxb: ',p^.namxb); p := p^.tiep; end; end; procedure liet_ke(t: trotg; ma: word); begin if t = nil then writeln('Khong tim thay'); else if t^.matg = ma then begin oten := t^.tentg; osach := t^.sachtg; xuat; end else if ma > t^.matg then liet_ke(t^.phai, ma) else liet_ke(t^.trai, ma); end; procedure loai_bo(var t: trotg); var r,p: trotg; begin if t <> nil then begin {Thăm nút = loại bỏ gốc} if t^.sachtg = nil then begin p := t; if t^.trai = nil then t := t^.phai else begin {Tìm nút cực phải của T} r := t^.trai; while r^.phai <> nil do r := r^.phai; {Chỉnh lại các mối nối} r^.phai := t^.phai; t := t^.trai; end; dispose(p); end; loai_bo(t^.trai); loai_bo(t^.phai); end; end; procedure bo_sung(var t: trotg; ma: word; tg,ten: string; nam: word); var s: tros; r,p: trotg; ok: boolean; begin if t = nil then begin {Tạo mới 1 tác giả} new(s); s^.tens := ten; s^.namxb := nam; s^.tiep := nil; new(t); t^.matg := ma; t^.tentg := tg; t^.sachtg := s; t^.trai := nil; t^.phai := nil; end else begin {Tìm nút P mà P^.Trai = NIL hoặc P^.Phai = NIL} p := t; ok := true; while (p^.matg <> ma) and ok do begin if ma > p^.matg then GIẢI ĐỀ THI CÁC NĂM Laptrinh_Hieu 27 if p^.phai <> nil then p := p^.phai else ok := false else if p^.trai <> nil then p := p^.trai else ok := false end; {Không tìm thấy P như trên. Nghĩa là OK = True } if p^.matg = ma then begin {Kiểm tra sách đã có chưa?} s:= p^.sachtg; while (s <> nil) or ((s^.tens<>ten ) and (s^.namxb<> nam)) do s := s^.tiep; if s = nil then begin new(s); s^.tens := ten; s^.namxb := nam; s^.tiep := p^.sachtg; p^.sachtg := s; end; end else {Đã tìm thấy P như trên} begin new(s); s^.tens := ten; s^.namxb := nam; s^.tiep := nil; new(r); r^.matg := ma; r^.tentg := tg; r^.sachtg := s; {Lựa chọn đưa vào trái hay phải} if ma > p^.matg then p^.phai := r else p^.trai := r; end; end; end; ĐỀ 2005 CÂU 3A procedure timten(trokhach: khach; n: word); var t:khach; begin if trokhach = nil then writeln('khong tim thay') else if trokhach^.sdt = n then begin write(trokhach^.ten); write(trokhach^.sonha); writeln(trokhach^.duong); end else if n > trokhach^.sdt then timten(trokhach^.phai,n) else timten(trokhach^.trai,n); end; Lời nhắn của tác giả: Đối tượng của tài liệu này là các sinh viên năm thứ 4, chuẩn bị thi tốt nghiệp. Tuy nhiên tất cả các bạn sinh viên năm thứ 2, 3, 4 đều có thể dùng được. Môn Phương pháp lập trình là sự kết hợp của môn Cấu trúc dữ liệugiải thuật với môn Ngôn ngữ lập trình Pascal (hoặc C/C ++ ) Sinh viên của các khoá từ trước đến nay thường rất ít khi cài đặt các thuật toán trong môn CTDL&GT nên thường bị bối rối khi chuẩn bị thi tốt nghiệp. Vì vậy nên số lượng sinh viên rớt tốt nghiệp vẫn còn cao. Các sinh viên khoá sau lấy đây là một bài học, nhưng đừng làm theo. Vừa học môn CTDL&GT vừa cài đặt các thuật toán đã học là một thói quen tốt, nó sẽ giúp cho bạn học tốt hơn môn học này, đồng thời ôn lại môn NNLT, chuẩn bị tốt cho kỳ thi tốt nghiệp cuối khoá. Khi nào không cài đặt được các thuật toán trong sách CTDL&GT thì có thể bạn sẽ cần tham khảo tài liệu này. Lưu ý: Tài liệu này chưa được tổ chức nào kiểm duyệt và bảo trợ. Huế, tháng 5 năm 2008. Nguyễn Viết Hiếu . := tim(T^.l, n); end; {Câu a} function dem(t:tro):word; begin if t = nil then dem := 0 else dem := 1+ dem(t^.l)+ dem (t^.r); end; {Câu a} procedure bosung(var. var i,j,dem: word; begin {xac dinh mien lien thong} fillchar(mien,100,0); dem := 0; for i:=1 to n do if mien[i] = 0 then begin dem := dem+1; try(i,dem);

Ngày đăng: 02/12/2013, 09:11

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan