Đề 1: Tính tổng các số dương / âm / all
Về cơ bản thì 3 cái này giống nhau, chỉ có điều kiện là khác nên chỉ cần học 1 cái là đủ. Chú ý điều kiện( in đậm)
1) Số dương
Program tong;
Uses crt;
Var i,n,t:integer;
a:array[1..10000] of integer;
BEGIN
clrscr;
writeln(‘Nhap n:’);
readln(n);
for i:=1 to n do
begin
write(‘a[‘,i,’]=’);
readln(a);
end;
t:=0;
for i:=1 to n do
if a[i] > 0 then t:=t a[i];
Writeln(‘Tong cac so duong la:’,t);
readln;
END.
2) Số âm
Program tong;
Uses crt;
Var i,n,t:integer;
a:array[1..10000] of integer;
BEGIN
clrscr;
writeln(‘Nhap n:’);
readln(n);
for i:=1 to n do
begin
write(‘a[‘,i,’]=’);
readln(a[i]);
end;
t:=0;
for i:=1 to n do
if a[i] < 0 then t:=t a[i];
Writeln(‘Tong cac so am la:’,t);
readln;
END.
3) Tất cả
Program tong;
Uses crt;
Var i,n,t:integer;
a:array[1..10000] of integer;
BEGIN
clrscr;
writeln(‘Nhap n:’);
readln(n);
for i:=1 to n do
begin
write(‘a[‘,i,’]=’);
readln(a[i]);
end;
t:=0;
for i:=1 to n do [i] (Chú thích: không có điều kiện)
Writeln(‘Tong cac so la:’,t);
readln;
END.
Đề 2: Tính trung bình cộng của tất cả các số
Bài này y chang bài tổng, chỉ thêm 1 dòng thôi
Program tbc;
Uses crt;
Var i,n,t:integer;
a:array[1..10000] of integer;
BEGIN
clrscr;
writeln(‘Nhap n:’);
readln(n);
for i:=1 to n do
begin
write(‘a[‘,i,’]=’);
readln(a[i]);
end;
t:=0;
for i:=1 to n do
t:=t/n
Writeln(‘Trung binh cong cac so la:’,t:4:2);
readln;
END.
Đề 3: Tìm số lớn nhất, bé nhất trong mảng:
Program lonbe;
Uses crt;
Var i,n,max,min:integer;
a:array[1..10000] of integer;
BEGIN
clrscr;
writeln(‘Nhap n:’);
readln(n);
for i:=1 to n do
begin
write(‘a[‘,i,’]=’);
readln(a[i]);
end;
max:=a[1];min:=a[1];
for i:=2 to n do
begin
if a[i] > max then max:=a[i];
if a[i] < min then min:= a[i];
end;
Writeln(‘Tong cac so duong la:’,t);
readln;
END.
Đề 4: In ra, Sắp xếp các số trong mảng
Hai cái giảm và tăng nhu nhâu, lại chỉ khác có điểu kiện
1) Giảm dần
Program sapxep;
Uses crt;
Var i,j,n,tam:integer;
a:array[1..10000] of integer;
BEGIN
clrscr;
writeln(‘Nhap n:’);
readln(n);
for i:=1 to n do
begin
write(‘a[‘,i,’]=’);
readln(a[i]);
end;
(In ra: )
for i:=1 to n do write(a[i],’ ‘);
(Sắp xếp: )
for i:=1 to n-1 do
for j:= i 1 to n do
if a[i]>a[j] then
begin
tam := a[i];
a[i] := a[j];
a[j] := tam;
end;
(In ra: )
writeln(‘ mang sau khi sap xep:’);
for i:=1 to n do write(a[i],’ ‘);
readln;
END.
2) Tăng dần
Program sapxep;
Uses crt;
Var i,j,n,tam:integer;
a:array[1..10000] of integer;
BEGIN
clrscr;
writeln(‘Nhap n:’);
readln(n);
for i:=1 to n do
begin
write(‘a[‘,i,’]=’);
readln(a[i]);
end;
(In ra: )
for i:=1 to n do write(a[i],’ ‘);
(Sắp xếp: )
for i:=1 to n-1 do
for j:= i 1 to n do
if a[i]<a[j] then
begin
tam := a[i];
a[i] := a[j];
a[j] := tam;
end;
(In ra: )
writeln(‘ mang sau khi sap xep:’);
for i:=1 to n do write(a[i],’ ‘);
readln;
END.