Những câu hỏi liên quan
Huy Phạm
Xem chi tiết
Kiều Vũ Linh
3 tháng 5 2023 lúc 6:12

1)

Var array:[1..1000] of integer;

i,n,t:integer;

Begin

Write('n = ');readln(n);

For i:=1 to n do

Begin

Write('Nhap so thu ',i,' = ');readln(a[i]);

End;

For i:=1 to n do

If a[i] > a[i+1] then

Begin

t:=a[i];

a[i]:=a[i+1];

a[i+1]:=t;

End;

Write('Sap xep tang dan ');

For i:=1 to n do write(a[i]:8);

Readln

End.

Bình luận (0)
Kiều Vũ Linh
3 tháng 5 2023 lúc 6:13

2)

Var array:[1..1000] of integer;

i,n,t:integer;

Begin

Write('n = ');readln(n);

For i:=1 to n do

Begin

Write('Nhap so thu ',i,' = ');readln(a[i]);

End;

For i:=1 to n do

If a[i] < a[i+1] then

Begin

t:=a[i];

a[i]:=a[i+1];

a[i+1]:=t;

End;

Write('Sap xep giam dan ');

For i:=1 to n do write(a[i]:8);

Readln

End.

Bình luận (0)
ádfg
Xem chi tiết
Nguyễn Lê Phước Thịnh
10 tháng 12 2021 lúc 23:11

#include <bits/stdc++.h>

using namespace std;

long long a[100],b[100],c[100],n,i,dem1,dem2;

int main()

{

cin>>n;

for (i=1; i<=n; i++)

cin>>a[i];

dem1=0;

dem2=0;

for (i=1; i<=n; i++)

{

if (a[i]%2==0) 

{

dem1++;

b[dem1]=a[i];

}

else 

{

dem2=0;

c[dem2]=a[i];

}

}

sort(b+1,b+dem1+1);

sort(c+1,c+dem2+1);

for (i=1; i<=dem1; i++)

cout<<b[i]<<" ";

for (i=dem2; i>=1; i--)

cout<<c[i]<<" ";

return 0;

}

Bình luận (0)
Asuna
Xem chi tiết

Dưới đây là mã chương trình Pascal để sắp xếp dãy số theo yêu cầu đã cho:

```pascal
program sorting;

const
MAX_N = 1000;

var
N, i, j, temp: integer;
arr: array[1…MAX_N] of integer;
oddArr, evenArr: array[1…MAX_N] of integer;
oddCount, evenCount: integer;
inputFile, outputFile: text;

begin
// Mở file input và đọc dữ liệu
assign(inputFile, 'sorting.inp');
reset(inputFile);
readln(inputFile, N);
for i := 1 to N do
read(inputFile, arr[i]);
close(inputFile);

// Sắp xếp mảng theo yêu cầu
oddCount := 0;
evenCount := 0;
for i := 1 to N do
begin
if arr[i] mod 2 = 1 then
begin
oddCount := oddCount + 1;
oddArr[oddCount] := arr[i];
end
else
begin
evenCount := evenCount + 1;
evenArr[evenCount] := arr[i];
end;
end;

// Sắp xếp mảng số lẻ tăng dần
for i := 1 to oddCount - 1 do
for j := i + 1 to oddCount do
if oddArr[i] > oddArr[j] then
begin
temp := oddArr[i];
oddArr[i] := oddArr[j];
oddArr[j] := temp;
end;

// Sắp xếp mảng số chẵn giảm dần
for i := 1 to evenCount - 1 do
for j := i + 1 to evenCount do
if evenArr[i] < evenArr[j] then
begin
temp := evenArr[i];
evenArr[i] := evenArr[j];
evenArr[j] := temp;
end;

// Mở file output và ghi kết quả
assign(outputFile, 'sorting.out');
rewrite(outputFile);
for i := 1 to oddCount do
write(outputFile, oddArr[i], ' ');
writeln(outputFile);
for i := 1 to evenCount do
write(outputFile, evenArr[i], ' ');
close(outputFile);
end.
```

Bạn có thể sao chép mã chương trình trên vào một tệp tin có tên `sorting.pas`, sau đó tạo một tệp tin `sorting.inp` và nhập dữ liệu theo định dạng đã cho. Chạy chương trình và kết quả sẽ được ghi vào tệp tin `sorting.out`.

Bình luận (0)
Phạm Dương Phúc Khang
21 tháng 1 lúc 22:55

var i,n:longint; a:array[1..1000] of longint;

begin

readln(n);

for i:=1 to n do read(a[i]);

for i:=1 to n do

     if a[i] mod 2=0 then 

         begin

              inc(k);

              b[k]:=a[i];

         end

else

begin

inc(t);

c[t]:=a[i];

end;

for i:=1 to k-1 do

for j:=i+1 to k do

if b[i]<b[j] then

begin

d:=b[i];

b[i]:=b[j];

b[j]:=d;

end;

for i:=1 to  t-1 do

for j:=i+1 to t do

if c[i]>c[j] then

begin

d:=c[i];

c[i]:=c[j];

c[j]:=d;

end;

for i:=1 to k do write(b[i],' ');

for i:=1 to t do write(c[i],' ');

end.

Bình luận (0)
Hùngg Nguyễnn
Xem chi tiết

Program Day_So_Giam_Dan;
Begin
Write(‘a[‘,i,’]= ‘);
End;
Readln;

End.

Bình luận (2)
#include <stdio.h> int main(){    int a[100];    int n;    printf("\nNhap so luong phan tu n = ");    do{        scanf("%d", &n);        if(n <= 0){            printf("\nNhap lai n = ");        }    }while(n <= 0);        for(int i = 0; i < n; i++){        printf("\nNhap a[%d] = ",i);        scanf("%d", &a[i]);    }        // Sap xep dung thuat toan sap xep chon    int tg;    for(int i = 0; i < n - 1; i++){        for(int j = i + 1; j < n; j++){            if(a[i] > a[j]){                // Hoan vi 2 so a[i] va a[j]                tg = a[i];                a[i] = a[j];                a[j] = tg;                    }        }    }            printf("\nMang da sap xep la: ");    for(int i = 0; i < n; i++){        printf("%5d", a[i]);    }        } 
Bình luận (0)
Nguyễn Lê Phước Thịnh
21 tháng 4 2021 lúc 19:30

uses crt;

var a:array[1..30]of integer;

i,j,tam:integer;

begin

clrscr;

for i:=1 to 30 do 

  begin

write('A[',i,']='); readln(a[i]);

end;

for i:=1 to 29 do 

  for j:=i+1 to 30 do 

  if a[i]>a[j] then 

begin

tam:=a[i];

a[i]:=a[j];

a[j]:=tam;

end;

for i:=1 to 30 do 

  write(a[i]:4);

readln;

end.

Bình luận (0)
k_o_t_en
Xem chi tiết
༺ミ𝒮σɱєσиє...彡༻
Xem chi tiết
nuqueH
13 tháng 5 2022 lúc 21:41

tham khảo:

uses crt;

var n,i:integer;

a:array[1..100] of integer;

begin

clrscr;

write(‘nhap so phan tu cua day:’);readln(n);

for i:=1 to n do

begin

write(‘a[‘,i,’]=’);readln(a[i]);

end;

writeln(‘day so da nhap:’);

for i:=1 to n do write(a[i]:2);

readln;

end.

Bình luận (1)
Minh
13 tháng 5 2022 lúc 21:42

tham khảo

uses crt;

var n,i:integer;

a:array[1..100] of integer;

begin

clrscr;

write(‘nhap so phan tu cua day:’);readln(n);

for i:=1 to n do

begin

write(‘a[‘,i,’]=’);readln(a[i]);

end;

writeln(‘day so da nhap:’);

for i:=1 to n do write(a[i]:2);

readln;

end.

Bình luận (0)
Nguyễn Lê Phước Thịnh
13 tháng 5 2022 lúc 23:05

uses crt;

var a:array[1..100]of integer;

i,n,tam,j:integer;

begin

clrscr;

readln(n);

for i:=1 to n do readln(a[i]);

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;

for i:=1 to n do write(a[i]:4);

readln;

end.

Bình luận (0)
Minh Lệ
Xem chi tiết
Thanh An
18 tháng 7 2023 lúc 21:11

THAM KHẢO!

1.Thuật toán sắp xếp chèn (Insertion Sort):

def insertion_sort(arr):

  for i in range(1, len(arr)):

   key = arr[i]

   j = i - 1

   while j >= 0 and arr[j] > key:

    arr[j + 1] = arr[j]

    j -= 1

   arr[j + 1] = key

  return arr

A = [5, 8, 1, 0, 10, 4, 3]

sorted_A = insertion_sort(A)

print("Dãy A sau khi sắp xếp chèn:", sorted_A)

2. Thuật toán sắp xếp chọn (Selection Sort):

def selection_sort(arr):

  for i in range(len(arr)):

   min_idx = i

   for j in range(i + 1, len(arr)):

    if arr[j] < arr[min_idx]:

     min_idx = j

   arr[i], arr[min_idx] = arr[min_idx], arr[i]

  return arr

A = [5, 8, 1, 0, 10, 4, 3]

sorted_A = selection_sort(A)

print("Dãy A sau khi sắp xếp chọn:", sorted_A)

3.Thuật toán sắp xếp nổi bọt (Bubble Sort):

def bubble_sort(arr):

  n = len(arr)

  for i in range(n - 1):

   for j in range(n - 1 - i):

    if arr[j] > arr[j + 1]:

     arr[j], arr[j + 1] = arr[j + 1], arr[j]

  return arr

A = [5, 8, 1, 0, 10, 4, 3]

sorted_A = bubble_sort(A)

print("Dãy A sau khi sắp xếp nổi bọt:", sorted_A)

Bình luận (0)
Jhon wisk
Xem chi tiết
Kiều Vũ Linh
25 tháng 4 2023 lúc 7:31

Var a:array[1..1000] of integer;

i,m,tam:integer;

Begin

Write('m = ');readln(m);

For i:=1 to m do

Begin

Write('Nhap so thu ',i,' = ');readln(a[i]);

End;

For i:=1 to m do

If a[i] < a[i+1] then

Begin

tam:=a[i];

a[i]:=a[i+1];

a[i+1]:=tam;

End;

Write('Mang sau khi sap xep: ');

For i:=1 to m do

Write(a[i]:8);

Readln;

End.

Bình luận (0)
Hồ Hoàng Long
Xem chi tiết