You are on page 1of 6

1

Bai da thuc uses crt; type dathuc = ^sohang; sohang = record heso:integer; somu:byte; link: dathuc; end; {Ham tinh gia tri cua da thuc khi biet x} Function giatri(dt:Dathuc; x:real):real; var s,sm:real; p:dathuc; lt:real; Begin s:=0; lt:=1; sm:=0; p:=dt; while p<>nil do Begin lt:=lt* exp((p^.somu-sm)*ln(x)); sm:=p^.somu; s:=s+p^.heso*lt; p:=p^.link; end; giatri:=s; end; {Ham cong 2 da thuc} procedure noidathuc(var p,cuoi:dathuc; q:dathuc); begin if p=nil then p:=q else cuoi^.link:=q; cuoi:=q; end; Function cong(p1,p2:dathuc):dathuc; Var p,q,x,z,y,cuoi:dathuc; Begin p:=nil; cuoi:=nil; x:=p1; y:=p2; while (x <> nil) and (y <> nil) do begin new(q); if (x^.somu=y^.somu) then begin if (x^.heso + y^.heso) <> 0 then begin q^.heso:=x^.heso + y^.heso; q^.somu:=x^.somu; q^.link:=nil; noidathuc(p,cuoi,q); end; x:=x^.link; y:=y^.link; end else if x^.somu<y^.somu then begin q^.heso:=x^.heso; q^.somu:=x^.somu; q^.link:=nil; noidathuc(p,cuoi,q); x:=x^.link; end else if x^.somu>y^.somu then begin q^.heso:=y^.heso; q^.somu:=y^.somu; q^.link:=nil; noidathuc(p,cuoi,q); y:=y^.link; end; {them vao cuoi ds moi} end; {lay phan du dua vao cuoi ds p}

2
if x=nil then z:=y else z:=x; while z <> nil do begin noidathuc(p,cuoi,z); z:=z^.link; end; cong:=p; end;

program caythumuc; uses crt; type cayTM=^TM; TM = record name:String; size:integer; con,em:cayTM; end; Function TimDD(goc:cayTM;x:String; dd:string):String; var q:hangdoi; found:boolean; p,f:cayTM; tg:string; begin if goc=nil then TimDD:='' else begin if (goc^.name=x) and (goc^.size<>-1) then timdd:=dd else begin tg:=TimDD(goc^.con,x,dd+'\'+goc^.name); if tg='' then TimDD:=TimDD(goc^.em,x,dd) else timDD:=tg; end; end; end; Function Timcha(goc:cayTM;x:String; var cha:cayTM):cayTM; var q:hangdoi; found:boolean; tg,p,f:cayTM; begin if goc=nil then Timcha:=nil else begin if (goc^.name=x) then timcha:=cha else begin tg:=Timcha(goc^.con,x,goc); if tg=nil then Timcha:=Timcha(goc^.em,x,goc) else timcha:=tg; end; end; end;

procedure BFS(goc:cayTM); {duyet theo chieu rong} var p:cayTm; q:hangdoi; x:String; begin

3
newhangdoi(q); p:=goc; add(q,p); while isEmpty(q)=false do begin p:=remove(q); writeln(p^.name); p:=p^.con; while p<>nil do begin add(q,p); p:=p^.em; end; end; end; procedure xoaTM(var goc:cayTM;x:String); var q:cayTM; begin if goc<>nil then begin if goc^.name=x then goc:=nil else begin q:=timcha(goc,x,goc); if q <> nil then begin if q^.con^.name=x then q^.con:=nil else q^.em:=q^.em^.em; end; end; end; end;

program mangcot; Procedure doi2cot(var a:mang2c; m, n, l,k:integer); Var i,tg:integer; begin for i:=1 to m do begin tg:=a[i,l]; a[i,l]:=a[i,k]; a[i,k]:=tg; end; end; procedure chanlecot(var a:mang2c; m,n:integer); var i,j,l,r,tg:integer; begin for i:=1 to n do begin l:=1; r:=m; while l<=r do begin while a[l,i] mod 2 = 0 do l:=l+1; while a[r,i] mod 2 = 1 do r:=r-1; if l< r then begin tg :=a[l,i]; a[l,i]:=a[r,i]; a[r,i]:=tg; end; end; end;

4
end; procedure sxtangcot(var a: mang2c ; m, n:integer); var i,j,d,c,tg:integer; s:mang1c; Begin {Tinh tong cac cot} for i:=1 to n do begin s[i]:=0; for j:=1 to m do s[i]:=s[i]+a[j,i]; end; {sap xep va doi cac cot} for i:=1 to n-1 do for j:=i+1 to n do if s[i] > s[j] then begin doi2cot(a,m,n,i,j); tg:=s[i]; s[i]:=s[j]; s[j]:=tg; end; chanlecot(a,m,n); End; Program manghang; procedure chanlehang(var a :mang2c; m,n:integer); var i,j,l,r,tg:integer; begin for i:=1 to m do begin l:=1; r:=n; while l<=r do begin while a[i,l] mod 2 = 0 do l:=l+1; while a[i,r] mod 2 = 1 do r:=r-1; if l<r then begin tg:=a[i,l]; a[i,l]:=a[i,r]; a[i,r]:=tg; end; end; end; end; Procedure doi2hang(var a:mang2c; m, n, l,k:integer); Var i,tg:integer; begin for i:=1 to n do begin tg:=a[l,i]; a[l,i]:=a[k,i]; a[k,i]:=tg; end; end; procedure sxtanghang(var a: mang2c ; m, n:integer); var i,j,tg:integer; s:mang1c; Begin {Tinh tong cac hang} for i:=1 to m do begin s[i]:=0; for j:=1 to m do s[i]:=s[i]+a[i,j]; end; {sap xep va doi cac dong} for i:=1 to m-1 do for j:=i+1 to m do if s[i] > s[j] then begin doi2hang(a,m,n,i,j); tg:=s[i]; s[i]:=s[j]; s[j]:=tg; end;

5
End; Program dothi; uses crt; type mang2c=array [1..10,1..10] of byte; dothi = record sodinh:byte; mtk:mang2c; end; var g:dothi; chon:array [1..10] of boolean; TPLT:array [1..10] of byte; soTPLT:byte; procedure DFS(g:dothi;v:byte); {duyet theo chieu sau} var u:byte; begin chon[v]:=true; TPLT[v]:=soTPLT; for u:=1 to g.sodinh do if (g.mtk[u,v]<>0) and (chon[u]=false) then DFS(g,u); end; procedure Lienthong(g:dothi); var i,j,v:byte; begin for v:=1 to g.sodinh do chon[v]:=false; soTPLT:=0; for v:=1 to g.sodinh do if chon[v]=false then begin soTPLT:=soTPLT+1; DFS(g,v); end; Writeln('So thanh phan lien thong: ', soTPLT); Writeln('Cac TPLT:'); for i:=1 to soTPLT do begin writeln('TPLT thu ',i,':'); for j:=1 to g.sodinh do if TPLT[j]=i then write(j); writeln; end; end;

procedure tatcaDD(g:dothi;x,y,k:byte); {tat ca dd khong qua k dinh} var d:array [1..100]of byte; dem,i,j:byte; {cac thu tuc con} procedure inDD(i:byte); var k:byte; begin for k:=1 to i do write(d[k]:4); writeln; end; procedure timDD(g:dothi;y,i:byte); var j:byte; begin for j:=1 to g.sodinh do if (g.mtk[d[i-1],j]<>0) and (not chon[j]) then begin d[i]:=j; chon[j]:=true; dem:=i; if (j=y) and (dem<=k) then

6
inDD(i) else timDD(g,y,i+1); chon[j]:=false; end; end; {Neu tim tat cac cac duong di thi bo bien dem} {thu tuc chinh} begin for i:=1 to g.sodinh do chon[i]:=false; d[1]:=x; chon[x]:=true;dem:=0; timDD(g,y,2); end;

You might also like