1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài tập pascal : Lời giải, đáp án part 2 doc

25 373 3
Tài liệu được quét OCR, nội dung có thể không chính xác

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 25
Dung lượng 1,08 MB

Nội dung

Trang 1

iit; mup := round(exp(In(2)*i))-1; end; readin end {Bai 2.27} Program Phuong_trinh_bac_hai_nghiem_phuc; var a,b,c,delta:real, BEGIN

write(‘Nhap vao he so (a,b,c): ');readin(a,b,c);

'writeln(Phuong trinh: f(x)= (‘,a,")"x"x + (b,')*x + (.e,) = delta:=b*b-4*a*c;

if delta<0 then begin

writeln(‘Phuong trinh co hai nghiem phục phan biet);

writeln(' x1= 'b/(2*a),'+i!,sqt(abs(delta))/2/a); writeln(' x2= ', -b/(2*a),'-i',sqrt(abs(delta))/2/a); end else begin

if delta=0 then write(‘phuong trinh co nghiem kep x1=; else writeln(‘Phuong trinh co hai nghiem phan biet x1= ',

(-b+sqrt(delta))/2/a,' x2= ', (-b-sqrt(delta))/2/a); end; readin; END -b/(2*a)) {Bai 2.28} Program Bat_phuong_trinh_bac_hai; var a,b,c,delta:real; BEGIN

'write(Nhap vao he so (a,b,c): ');readin(a,b,

Trang 2

writeln(Phuong trinh: f(x)= (,a,)x^2 + (,b,)x + (e;) > 09;

delta:=b*b-4*a*c; if delta<O then

begin if(a>0) then

wiiteln(Bat phuong trinh tren dung voi moi x thuoc R'); if(a<0) then

writeln(‘Khong co gia tri x nao thoa man bat phuong trinh tren’); end

else if delta=0 then begin

if(a>0) then

write(‘Bat phuong trinh nghiem dung voi moi x <>'-b/(2*a));

if(a<0) then

write(Bat phuong trinh vo nghiem));

end

else if delta>0 then begin

if(a>0) then writeln(Bat phuong trinh nghiem dung voi moi x <',

(-b-sqrt(delta))/2/a,' hoac x > ', (-b+sqrt(delta))/2/a); if(a<0) then writeln(Bat phuong trinh nghiem dung voi ',

(-b-sqrt(delta))/2/a,' < x <', (-b+sqrt(delta))/2/a); end; readin; END {Bai 2.29} Program Phuong_trinh_trung_phuong; var a,b,c,delta:real; BEGIN

write('Nhap vao he so (a,b,c): );readin(a,b,c); wiiteln(Phuong trinh: f(x)= ax^4 + bx^2 + c = 0);

delta:=b"b-4*a*c;

if delta<0 then write(‘phuong trinh vo nghiem')

Trang 3

else if delta=0 then begin

if -b/(2*a)>=0 then write(‘phuong trinh co 2 nghiem x1 = ,

sqrt(-b/(2*a)),' x2 = ',-sqrt(-b/(2*a)))

else write(Phuong trinh vo nghiem’); end else (*delta >0 *) begin if (-b+sqrt(delta))/2/a>=0 then writeln(x1= ',sqrt((-b+sqrt(delta))/2/a), 'x2= ',-sqrt((-b+sqrt(delta))/2/a)) else write(Phuong trinh vo nghiem); if (-b-sqrt(delta))/2/a>=0 then writeln(x3= ',sqrt((-b+sqrt(delta))/2/a), 'x4= ',-sqrt((-b+sqrt(delta))/2/a)); end; readin; END {Bai 2.30} Program He_2_an; var a,b,¢,d,p,q,dt:real; BEGIN

write('Nhap vao gia tri (a,b,p): '); readin(a,b,p);

write(‘Nhap vao gia tri (c,d,q): '); readin(c,d,q);

dt:=a"d-b*c;

if (dt=0)then write('He phuong trinh vo dinh hoac vo nghiem')

Trang 4

{Bai 2.32}

Program In_ky_tu; var t:char; BEGIN

Write(Nhap vao mot ky tu: '); readin(t);

case t of

‘A’ 'Z': WriteLn(‘Chu HOA’);

'a z': WriteLn('Chu thuong);

'0 9: WriteLn('Chu so); else

WiiteLn('Chao tam biet);

end; readin; ' END {Bai 2.33} Program Tinh_tong; var n,s,i:integer; BEGIN

write(Nhap vao n: '); readin(n);

S:=0;

for Ï:=1 to n do s:=s+i;

if s=n"(n+1)/2 then write('Cong thuc dung va ='s); readin; END {Bai 2.34} Program Tinh_tong_bp; var n,s,i:integer; BEGIN

write(Nhap vao n: '); readin(n);

Trang 5

for ï:=1 to n do s:=s+ii;

if s=n*(n+1)*(2*n+1)/6 then write('Cong thuc dung va

readin; END {Bai 2.35} Program Tinh_to_hop; var n,m,C,i,gtn,gtm,gtmn:integer; BEGIN

write('Nhap vao n,m: '); readin(n,m);

gin:=1;gtm:=1;gtmn:=1; for ï:=1 to n do gtn:=gtn*i; for to m do gtm:=gtm°i, for ï:=1 to n-m do gtmn:=gtmnfi; write(‘Cn(m) =',gtn/(gtm*gtmn)); readin; END {Bai 2.36} Program Viet_so; var M,S,ij:integer, BEGIN for i:=3 to 8 do for j:=2 to 9 do write(i,j,', ');

for i:=10 to 99 do S:=S+i;

write(‘Tong tu 10->99 ',S); M:

for i:=2 to 8 do if odd{i)<>true then M:=M"i; write(‘Tich cac so chan co 1 chu so= ',M); readin;

END

Trang 6

{Bai 2.37} Program Tinh_e; var t,ij,n:integer; e :real; BEGIN e=1; 'write(Nhap n = ');readIn(n); for i:=1 to n do end; write('e = ',e); readin; END {Bai 2.38} Program HCN_sao; var d,r,ij:integer; BEGIN

'write('Nhap chieu dai, rong (d,r) = ');readin(d,r); for i:=1 to d do write(*');writeln;

Trang 7

{Bai 2.39}

Program GT; var n,Ìj:integer; BEGIN

'write(Nhap chieu cao (n) = ');readln(n); for i:=1 tondo

begin

write(’ ':n-i+1);

for j:=1 to 2*i-1 do write(*);

writeln;

for i:=1 ton do begin write(' ':n-i#1); for j:=1 to i do write("* '); writeln; end; readin: END {Bai 2.40} Program So_n; var n,ij:integer; a,s ‘real; BEGIN

write('Nhap so thuc a = ');readin(a);

repeat +1 fi; i=i*1; until s>a;

'writeln(So n nho nhat tim duoc = ',Ì);

Trang 8

write('Tong ve trai S = ',S); readin; END {Bai 2.41} Program Can_100; var i:integer; BEGIN

writeln(Can bac 2 cua ');

for i:=1 to 100 do write(' 'i, la ',sqrt():4:2);

readin;

'writeln(Can bac 3 cua’);

for i:=1 to 100 do write(' 'j" la ',exp((1/3)*In():4:2);

readin;

writeln('Can bac 4 cua ');

for i:=1 to 100 do write(" i," la ',exp((1/4)*ln():4:2);

readin; END {Bai 2.42} Program So_le; var n,count:integer; BEGIN write(Nhap so tu nhien n= ‘);1 count:=0; while n>0 do begin

Trang 9

writeln(co ',count,, ' chu so le '); readin; END {Bai 2.43} Program ASCII; var ch ‘char, BEGIN

writein(' CHUONG TRINH DOC KY TU CHUYEN MA ASCII ');

repeat

write(Nhap vao mot ky tu));readln(ch);

writeln(‘Ky tu vua nhap co ma ASCII la: ',ord(ch));

{Bai 2.44}

Program BT;

var n,itj integer;

BEGIN

'writeln(' Tim so hoan hao ');

write(Nhap so n = ';readIn(n); for i:=2 to n do begin for to ï div 2 do ifi mod j=0 then t:=t+j;

if t=i then writein(t,' la so hoan hao’);

end;

readin; END

74

Trang 10

{Bai 2.45} Program TB;

var nji,tj ‘integer;

BEGIN

writeln(' Tinh trung binh ');

t;=0; Í:=0; repeat

'write(Nhap so nguyen n = ');readIn(n); t=ttn;

i:=i+1;

until n=0;

write(‘Trung binh cua day = ',t(i-1));

readin; END

{Bai 2,46}

Program chu_so; var n,count ‘integer; BEGIN

'write(Nhap so tu nhien n= ');readin(n); count:=0; while n>0 do begin count:=count+1; n:=n div 10; end;

writeln(‘co ‘count ,’ chu so ');

readin; END

Trang 11

{Bai 2.47}

Program b47;

uses crt;

var nzi,t,j sinteger; ch: char, a,b ‘real; BEGIN

repeat

€lrser;

Writeln(' TINH DIEN TICH VA THE TICH CAC HINH Mộ writeln('1 Dien tich hinh chu nhat yi

writeln(2 Dien tịch hinh tam giac ');

writeln('3 The tich hinh cau‘);

'writeln(4 Ket thuc ');

ch:sreadkey;

if ch="1' then

begin

write('1 Dien tích hinh chu nhat ');readin;

end; if ch='2' then

begin

write(2 Dien tich hinh tam giac ');readIn; end;

begin

write(’3 The tich hinh cau ');readin;

Trang 12

BEGIN

write(‘Nhap x = ');readin(x;;

write(Nhap eps (0<eps<1): ');readIn(eps);

t=1; =1; ex:=1; repeat ex:=extt until t<eps; write('e*x = ',ex); readin; END {Bai 2.49} Program Tinh_Sin_x; var i,j,n:integer; tsinx,x,eps :real; BEGIN write(Nhap x = ');readin(x);

write(Nhap eps (0<eps<1): ');readin(eps);

Trang 13

Sinx:=sinx+t;

iit;

until abs(t)<eps;

writeln(Tinh theo cong thuc Sin(x) = ',sinx);

write('Tinh theo ham chuan cua Pascal Sin(x) = ',sin(x));

readin; END {Bai 2.50} Program Tinh_Cos_x; integer; t,coSx,x,eps ‘real; BEGIN write('Nhap x = ');readin(x);

'write(Nhap eps (0<eps<1): ')\readin(eps);

repeat for j:=1 to n do begin t=t'0)); end; COSX:ZCOSX+{, +1; until abs(t)<eps;

'writeln('Tinh theo cong thuc Cos(x) = ',cosx);

'write(Tinh theo ham chuan cua Pascal Cos(x) = ',cos(x)); readin;

END

Trang 14

{Bai 2.51)

Program Tinh_So_pi;

var i:integer;

t,sopi,x,eps ‘real; BEGIN

writeln(' Chuong Trinh Tinh So Pi

'write(Nhap eps (0<eps<1): ');readln(eps); =1; sopi t=1; repeat *(Uabs(Đ)* 1/(2*i+1); sopi:=sopitt; bit; until abs(t)<eps;

writeIn(Tinh theo cong thuc Pi = ',sopi"4);

'write(Tinh theo ham chuan cua Pascal Pi = ',pi); readin; END {Bai 2.52} Program Tinh_I1_I2; var I1,12,i,S:integer; BEGIN repeat

write('Nhap so nguyen I1 ');readIn(l1);

if I1<=0 then writeln(Nhap lai I1 sao cho I1>0!);

until 11>0;

repeat

'write(Nhap so nguyen l2 = ');readIn(I2), if 12<11 then writeln(‘Nhap lai l2 sao cho l2>11'); until 12>11;

Trang 15

for begin

if odd(i) then S:=S+i;

end;

writeln('Tong gia trỉ tìm duoc = ',S);

readin; END +1 to 12-1 do {Bai 2.53} Program Tim_nghiem; var †, x,a,b,eps,f :real; BEGIN

'writeln(' CHUONG TRINH TINH NGHIEM CUA PHUONG TRINH’);

writeln(’ F(x) = x*x*x - 27 trong khoang [-10,10]);

write(’ Nhap gia tri sai so cua nghiem eps = "›;readIn(eps);

b:=10; repeat t=(b+a)/2; = (a*a"a-27)*(t"t"t-27); if f<=0 then b:=t else a:=t; until abs(a-b)<eps;

writeln('Tong gia tri tim duoc = ',t); readin;

END

Trang 16

Chương 3 Kiểu mảng và tập hợp

{Bai 3.1}

Program Day_so;

var i,n,count,id :integer,

a :array[1 100] of real; Tong, Tbinh,max :real;

BEGIN

'write(' So luong phan tu cua day so (n) = ');readin(n); Tong:=0;

for i:=1 ton do begin

write(' Nhap phan tu a[i,] = ');readIn(a[i]);

†ong:=tong+a[i];

end;

'writeln(' Tong cac phan tu cua day = ',tong); 'writeln(' Trung binh cong cua day la: ',tong/n); tong :=0; for tondo if afij>=0 then begin count:=count+1; tong:=tong+a[i]; end;

writein(' So luong so hang duong la ',count,' va tong : ',tong);

i:=0; repeat

=i*1;

unil a[ï]

'writeln(' Chi so cua so hang duong dau tien cua day: ',Ì);

Trang 17

begin

if afi]>max then

begin

end;

'writeln(' So lon nhat cua day la a[',id,] = ',max); readin;

END

{Bai 3.2}

Program So_nhi_phan;

var i,n,countid -integer;

a:array[1 100] of 0 1;

BEGIN -

'write(Nhap so thap phan can doi ra nhì phan (n) = ');readln(n);

=0, repeat

'write(' So nhi phan tuong ung = '); unt downto 0 do write(a[i]);

{Bai 3.3}

Program diem_tb:

var i,n:integer;

kha,gioi,trungbinh,vanduoi5,toanduoi5 :real; Toan,Van, Tb:array[1 100] of real;

Trang 18

BEGIN

write(Nhap so luong hoc sinh (n) = '};readin(n);

=0;

for ¡=1 to n do

begin

writeln('Hoc sinh thu ‘,i);

write(’ Diem mon Toan = ');readin(Toanfi]); write(' Diem mon Van = ');readin(Van[i]);

Tb[i]:=(Toan[i|+van[i))/2; end; kha:=0; gioi:=0; trungbinh:=0; vanduoi5:=0; toanduoi5:=0; for i:=1 tondo begin

if tb{i]>=8 then gioi:=gioi+1;

if (7<=tb[i]) and (tb[i}<8) then kha:=kha+1;

if (5<=tb[i]) and (tb[i]<7) then trungbinh:=trungbinh+1; if van[i]<5 then vanduoi5:=vanduoi5+1;

if toan[i]<5 then toanduoi5:=toanduoi5+1; end;

writeln(So luong hoc sinh xep loai gioi = ',gioi:2:0, ' chiem ",gioi*100/n:2:2,'%');

'writeln(So luong hoc sinh xep loai kha = ',kha:2:0, " chiem ',kha*100/n:2:2,'%');

'writeln('So luong hoc sinh xep loai trung binh = ',trungbinh:2:0,

' chiem ',trungbinh*100/n:2:2,'%)),

'writeln(So luong hoc bi mon Van duoi 5 = ',vanduoi5: * chiem ',vanduoi5*100/n:2:2,'%');

'writeln('So luong hoc bi mon Toan duoi 5 = ',toanduoi5:2:0, ‘ chiem ',toanduoi5*100/n:2:2,'%');

readin; END

Trang 19

{Bai 3.4}

Program Tan_xuat; var i,j,n.t ‘integer,

a:array[1 100] of integer;

daxet:array[1 100] of boolean;

BEGIN

'write(Nhap vao so luong phan tu cua day (n) = );readin(n); for i:=1 ton do

begin

write(’ Nhap phan tu thu ‘ daxet[i]:=false;

end;

fori:=1 tondo begin

t=1;

if not daxet{i] then begin for j:=i+1 to n do begin if a[i]FEa[i] then begin daxet[i]:=true; daxet{j]:=true; t=tr1; end; end;

writeln(‘Tan so xuat hien cua so ',a[i],' la: ',t);

Trang 20

{Bai 3.5}

Program Tan_xuat; var ij,n,khac :integer;

a,tarray[1 100] of integer; daxet:array[1 100] of boolean;

BEGIN

write(Nhap vao so luong phan tu cua day (n) = ');readin(n);

for tondo

begin

write(' Nhap phan tu thu 'i,' = ');readIn(a[i]);

daxet[i]:=false; en

for begin

if not daxet{i] then begin

for j:=i ton do

begin if afjJ=a[i] then begin daxet[i]:=true; daxet[j tondo

ton do if t[i]>0 then khac:=khac+1;

writeln(Co ',khac,' so hang khac nhau:’);

for i:=1 to n do

begin if t{i]>0 then

begin

write(So ',a], la: ');

for tondo

Trang 21

if afjJ=afi] then write(‘a['j,'] '); 'writeln; end; end; readin; END {Bai 3.6} uses crt; var n,max,tam,m : integer; ij,chen,xoa : integer;

a,b,c,d,e : array [1 100] of integer;

thay: boolean; Begin

clrscr;

write (' Nhap vao so phan tu cua day So : ');

readin(n); for i:= 1 ton do

begin

write (' Nhap vao gia cua cua phan tu thu ',i,' readln(a[l]);

end;

Trang 22

for if b{i] > b[j] then begin tam z= b[j]; ĐỊ]

writeln( ' Mang sau khi sap xep '); for Ï:= 1ton do

write ('', bfij,");

{***** Chen Phan Tu Vao Mang ***** } writein;

write (‘ Nhap Vao gia tri can chen vao mang : '); readIn(chen);

m:=n;

n=n+1;

a[n] := chen;

writeln (' Mang chua sap xep sau khi chen mot phan tu vao cuoi '); for ï:= 1ton do

write ( '", ai," ),

{**** Chen phan tu vao dung vi tri cua mang da sap xep ***** } writein;

write (' Nhap Vao gia tri can chen vao mang :'); readIn(chen);

fori := 1 to m do

if (chen < b[i]) and (thay = false) then

begin thay ;= true;

for j := m+1 downto i+1 do

bỊ] := b[-1],

bịi| := chen;

end;

if thay = false then b[n] := chen; writeln;

'writeln (' Mang sap xep sau khi chen ');

Trang 23

for i:= 1 tondo

write (‘", bfil, ");

{****** Tim phan tu max trong mang ***** }

fori tom do := abs(c[i]);

On

fori:=1tomdo if max < ci] then

max := c[i]; writeln;

'writeln(' Gia trí tuyet doi max cua mang la : ',max);

đttrtr99* xoa mọt phan tụ ra khoi mang ****** }

write (' Mang dang co ',m,' phan tu, ban muon xoa phan tu thu : ); readin(xoa);

fori := xoatom-1 do

fi] := dfi+1];

writeln,

Trang 24

{nhap tap hop a}

'write('So phan tu tap hop a:’); readin(m);

for i:=1 tom do

begin

write(Phan tu thu ', ¡, ' cua tap hop a : ');

readIn(a[i]);

end;

{ nhap tap hop b }

write('So phan tu tap hop b:’);

teadin(n);

for i:=1 ton do

begin

write(’Phan tu thu ', i, ‘cua tap hop b : ');

readIn(b[i]); end;

{phep giao: x ï A va x†B}

ks k: so phan tu tap hop ket qua c } fori := 1 tom do for tondo if afi] = bfj] then begin +; alll:

{viet ket qua }

'writeln('Giao cua 2 tap hop:’);

fori:= 1 tok do

writen('Phan tu thu’ i, ‘cua giao =', ci); {phep hop: x 1A hoac x1 B}

Trang 25

fori :=1tomdo if afi] = bj] then begin trung break; end;

if not trung then

begin

k;=k#st

tk] := bf); end;

end;

{ viet ket qua }

writein('Hop cua 2 tap hop:'); fori:=1tok do

writeln(Phan tu thu’, i, ‘cua hop =‘, c[i]); { phep hieu A\B: x7 A nhung x khong 1B }

k:=0; for Ì := 1 to m do begin trung ;= false; for j:= 1 tondo if afi] = bfj] then := true; trung := true;

if not trung then begin

{ viet ket qua }

writeln(‘Hieu cua 2 tap hoy fori tok do

writeln('Phan tu thu ', i, ‘cua hieu =", c[Í]); readin;

end

Ngày đăng: 24/07/2014, 03:21

TỪ KHÓA LIÊN QUAN

w