Pascal 8: Mảng 1 chiều

M

mytab

[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

Nhập vào một số nguyên N<=100. Tạo 1 dãy số có N số ngẫu nhiên có giá trị A <=100.
0. Sắp xếp dãy số tăng dần.
1. Liệt kê dãy số ra màn hình.
2. Liệt kê các số nguyên là số nguyên tố.
3. Liệt kê các số nguyên là số chính phương.
4. Tìm phần tử nào trong dãy số xuất hiện nhiều nhất, Nếu có nhiều hơn một phần tử thì liệt kê phần tử có giá trị nhỏ nhất.
 
M

megamanxza

Mã:
[B][SIZE="4"][COLOR="Blue"]
Uses crt;
var a, f: array [1..100] of integer;
       i, j, tg, dem, n, max: integer;
function KTNguyento (so: integer): boolean;
var z: integer;
  begin
    z:=2; while so mod z <> 0 do inc (z);
    if z=so then KT:=True else KT:=False;
    if so<2 then KT:=False;
  end;
function KTChinhphuong (so: integer): boolean;
var x: integer;
  begin
    x:=trunc(sqrt(so));
    if sqr(x)=so then KT:=True else KT:=False;
  end;
begin
clrscr;
write ('Nhap so nguyen N: '); readln (n);
for i:= 1 to n do 
  begin
    A[i] := random(101);
    inc (f(a[i]));
  end;
write ('Day so duoc tao la: ');
for i:= 1 to n do write (a[i],' ');
writeln;
dem:=0; for i:= 1 to n do
  if KTNguyento(a[i])=True then inc (dem);
if dem=0 then writeln ('Khong co so nguyen to nao trong mang!')
  else begin
    write ('Cac so nguyen to trong mang; ');
    for i:= 1 to n do
      if KTNguyento(a[i)=True then write (a[i],' ');
    writeln;
  end;
dem:=0; for i:= 1 to n do
  if KTChinhphuong(a[i])=True then inc (dem);
if dem=0 then writeln ('Khong co so chinh phuong nao trong mang!')
  else begin
    write ('Cac so chinh phuong trong mang; ');
    for i:= 1 to n do
      if KTChinhphuong(a[i)=True then write (a[i],' ');
    writeln;
  end;
max:=0;
for i := 1 to 100 do if F[i] > F[max] then max := i;
write('Phan tu xuat hien nhieu nhat la: ', max);
readln
end.[/COLOR][/SIZE][/B]

Thanks và Đúng cho mình nha!
 
L

lamdetien36

Mã:
[B][SIZE=2][COLOR=black]Uses crt;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]var a, f: array [1..100] of integer;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]       i, j, tg, dem, n, max: integer;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]function KTNguyento (so: integer): boolean;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]var z: integer;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  begin[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    z:=2; while so mod z <> 0 do inc (z);[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    if z=so then KT:=True else KT:=False;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    if so<2 then KT:=False;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  end;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]function KTChinhphuong (so: integer): boolean;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]var x: integer;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  begin[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    x:=trunc(sqrt(so));[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    if sqr(x)=so then KT:=True else KT:=False;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  end;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]begin[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]clrscr;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]write ('Nhap so nguyen N: '); readln (n);[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]for i:= 1 to n do [/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  begin[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    A[i] := random(101);[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    inc (f(a[i]));[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  end;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]write ('Day so duoc tao la: ');[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]for i:= 1 to n do write (a[i],' ');[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]writeln;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]dem:=0; for i:= 1 to n do[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  if KTNguyento(a[i])=True then inc (dem);[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]if dem=0 then writeln ('Khong co so nguyen to nao trong mang!')[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  else begin[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    write ('Cac so nguyen to trong mang; ');[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    for i:= 1 to n do[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]      if KTNguyento(a[i)=True then write (a[i],' ');[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    writeln;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  end;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]dem:=0; for i:= 1 to n do[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  if KTChinhphuong(a[i])=True then inc (dem);[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]if dem=0 then writeln ('Khong co so chinh phuong nao trong mang!')[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  else begin[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    write ('Cac so chinh phuong trong mang; ');[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    for i:= 1 to n do[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]      if KTChinhphuong(a[i)=True then write (a[i],' ');[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]    writeln;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]  end;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]max:=0;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]for i := 1 to 100 do if F[i] > F[max] then max := i;[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]write('Phan tu xuat hien nhieu nhat la: ', max);[/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=black]readln[/COLOR][/SIZE][/B]
[SIZE=2][COLOR=black][B]end.[/B][/COLOR][/SIZE]

Thanks và Đúng cho mình nha!
Còn phần sắp xếp tăng dần nữa mà .
 
M

megamanxza

Còn phần sắp xếp tăng dần nữa mà .

À quên! Cám ơn đồng chí Làm..., à quên, lamdetien đã nhắc!
Phần tăng dần thì bạn làm thế này:
Mã:
[B][COLOR="Blue"]
for i:= 1 to n-1 do
  for j:= i+1 to j do
    if a[i]>a[j] then
      begin
        tg:=a[i];
        a[i]:=a[j];
        a[j]:=tg;
      end;[/COLOR][/B]
P/s: có một sự thật là đối với các mem Tin học trẻ-er chúng ta thì việc sắp xếp tăng-giảm dần với một mảng là bài tập vỡ lòng luôn rồi nhỉ lamdetien? :cool:
 
S

syphuongcuong3

Bạn đùa à, có rất nhiều thuật toán sắp xếp từ đơn giản đến nâng cao với độ phức tạp giảm dần. Phương pháp sắp xếp lại mảng thì cũng là thuật toán sắp xếp mà =))
 
H

hoangdampc

Nói về thuật toán sắp xếp thì gồm
Nổi bọt(bubble sort)
Vun đống Heapsort
Sắp xếp nhanhQuick Sort
 
S

syphuongcuong3

Sắp xếp thì nhiều lắm: Insertion sort, selection sort, quick sort, heap sort, merge sort,...
 
Top Bottom