uses crt;
var a: array [1..50] of integer;
n, i, j: integer;
begin
clrscr;
write('Nhap so phan tu cua day so, N= ');
readln(n);
for i:=1 to n do
begin
write('Phan tu thu ',i,' = ');
readln(a);
end;
write(a[1],' ');
for i:=2 to n do
for j:=1 to i-1 do
begin
if a=a[j] then
break;
if j=i-1 then
write(a,' ');
end;
readln;
end.
Ý tưởng:
-Ban đầu tìm số khác tất cả các số trong dãy đó (có thể là số lớn nhất +1, hoặc số nhỏ nhất -1)
-Với mỗi giá trị của mảng, kiểm tra xem có trong mảng đó có số nào = nó ko(trừ nó ) thì gán số = nó đó là số tìm dc ở trên
-Kiểm tra xem số nào khác số tìm dc ở bước 1 thì in ra.
Code:
PHP:
Var A:array[1..100] of integer;
i1,j,i,s,n:integer;
Begin
i1:=1;
Write('Nhap so phan tu: ');readln(n);
For i:=1 to n do
Begin
write('Nhap A[',i,']');readln(a[i]);
If A[i]>A[i1] then i1:=i;
End;
s:=A[i1]+1; //s là 1 số khác tất cả các số trong dãy
For i:=1 to n do
Begin
For j:=1 to n do
If (i<>j) and (A[i]=A[j]) then A[j]:=s; // gán các số = nhau trừ số đầu tiên =s
If A[i]<>s then write(A[i]:3); // tìm số khác s viết ra
End;
Readln;
End.
Ý tưởng:
-Ban đầu tìm số khác tất cả các số trong dãy đó (có thể là số lớn nhất +1, hoặc số nhỏ nhất -1)
-Với mỗi giá trị của mảng, kiểm tra xem có trong mảng đó có số nào = nó ko(trừ nó ) thì gán số = nó đó là số tìm dc ở trên
-Kiểm tra xem số nào khác số tìm dc ở bước 1 thì in ra.
Code:
PHP:
Var A:array[1..100] of integer;
i1,j,i,s,n:integer;
Begin
i1:=1;
Write('Nhap so phan tu: ');readln(n);
For i:=1 to n do
Begin
write('Nhap A[',i,']');readln(a[i]);
If A[i]>A[i1] then i1:=i;
End;
s:=A[i1]+1;
For i:=1 to n do
Begin
For j:=1 to n do
If (i<>j) and (A[i]=A[j]) then A[j]:=s;
If A[i]<>s then write(A[i]:3);
End;
Readln;
End.
Còn đây là ý tưởng bài của mình:
- Ban đầu tìm tất cả các số bằng nhau có vị trí trong dãy...
- Rồi xoá các số đó.
PHP:
program BT1;
var i,n,s,j:integer;
a,b: array [1..1000] of integer;
begin
write('nhap n: '); readln(n);
for i:=1 to n do
begin
write('a[',i,']= ');
readln(a[i]);
end;
s:=0;
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]=a[j] then
begin
s:=s+1;
b[s]:=j;
end;
for i:=1 to s do
begin
for j:=b[i] to n-1 do
a[j]:=a[j+1];
n:=n-1;
end;
for j:=1 to n do
write(a[j],' ');
readln
end.