PASCAL
tìm UCLN,BCNN của dãy(quy định sử dụng dữ liệu kiểu file);
vd
f1(file input):nhập dãy
f2(file output):xuất ra UCLN và BCNN của dãy
chú ý: có thể sử dụng hàm kt hoặc ko cần
PASCAL
tìm UCLN,BCNN của dãy(quy định sử dụng dữ liệu kiểu file);
vd
f1(file input):nhập dãy
f2(file output):xuất ra UCLN và BCNN của dãy
chú ý: có thể sử dụng hàm kt hoặc ko cần
uses crt;
var i,n,t,tich:longint; a:array[1..10000]of longint; f1,f2:text;
function kt(b:longint):boolean;
var i:longint;
begin
kt:=true;
for i:=1 to n do if a[i] mod b<>0 then kt:=false;
end;
begin
clrscr;
assign(f1,’input.pas’);reset(f1);
readln(f1,n);
for i:=1 to n do read(f1,a[i]);
close(f1);
assign(f2,’output.pas’);rewrite(f2);
for i:=1 to a[1] do
if kt(i) then t:=i;
writeln(f2,’UCLN= ‘,t); tich:=1;
for i:=1 to n do tich:=tich*a[i];
writeln(f2,’BCNN= ‘,tich div t);
close(f2);
end.
var i,n,a,b, uc,bcnn:integer ;
c:array[1..100] of integer;
function ucln(a,b:integer):integer;
var r:integer;
begin
r:=a mod b;
while r<>0 do begin a:=b; b:=r; r:= a mod b; end;
ucln:=b;
end;
begin
assign(input,’f1.inp’); reset(input);
assign(output,’f2.out’); close(output);
readln(n);
for i:=1 to n do
begin readln(c[i]); end;
for i:= 1 to n do Write(c[i]:3);writeln;
uc:=ucln(c[1], c[2]);
bcnn:=(c[1]*c[2])div uc;
for i:=3 to n do
begin
uc:=ucln(uc,c[i]);
bcnn:= bcnn*c[i] div uc;
end;
Writeln(uc);
Write(bcnn);
close(input); close(output);
end.