Bài tập mảng 2
Sat Jun 22, 2024 10:12 am
Viết ct Pascal nhập vào 1 dãy số nguyên dương gồm 10 số. Trong đó có 5 số nguyên âm. TÍnh TBC 5 số nguyên dương còn lại (nếu ko đủ 5 số nguyên dương, yêu cầu nhập lại)
GIẢI
![tongue](https://2img.net/i/fa/i/smiles/icon_tongue.png)
GIẢI
- Code:
uses crt;
var
a:array[1..100] of integer;
x,n,i,dem:integer;
begin
clrscr;
repeat
writeln;
write('nhap vao so luong phan tu'); readln(n);
while n<0 do
begin
writeln('So luong phan tu khong duoc be hon 0,nhap lai'); readln(n);
end;
for i:=1 to n do
begin
write('a [',i,'] =');
readln(a[i]);
end;
write('Cac so vua nhap la: ');
for i:=1 to n do write(a[i],' ');
writeln;
write('Nhap vao gia tri x can tim '); readln(x);
dem:=0;
for i:=2 to n do
if a[i]=x then dem:=dem+1;
if dem=0 then write('(',x,') Khong co trong day so| hoac (',x,') chi co 1 phan tu trong day so ');
writeln;
if dem=0 then writeln('Vui long nhap lai ')
else
until dem>0;
begin
if dem=2 then writeln('Co ',dem,' phan tu bang voi ',x);
write(x,' o nhung vi tri :');
for i:=1 to n do
if a[i]=x then write(i,' ');
readln;
end;
end.
![tongue](https://2img.net/i/fa/i/smiles/icon_tongue.png)
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|