Bài 1. Cho dãy số a1,a2,...an. Sắp xếp thành dãy giảm dần Bài 2. Cho dãy a1,a2,a3...an gồm n số nguyên dương. -Sắp xếp các số chẵn về đầu hàng tăng dần -Sắp xếp các số lẻ về cuối hàng giảm dần GIÚP EM VỚI Ạ :(((
Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.
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`.
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.
Input: N và dãy số nguyên a1, a2,..., aN
Ouput: dãy số sắp xếp theo thứ tự tăng dần
B1: Nhập vào n và dãy số nguyên a1, . . . ,aN;
B2: M ← N;
B3: Nếu M<2 thì in dãy đã sắp xếp rồi kết thúc;
B4. M ← M – 1; i ← 0;
B5: i ← i + 1;
B6: Nếu i > M thì quay lại bước 3;
B7. Nếu ai > ai+1 thì tráo đổi cho nhau;
B8: Quay lại bước 5;
Program Tin_hoc;
Uses crt;
Var i,tam,n,chan,le,j:integer;
a,daychan,dayle:array[1..10000] of integer;
Begin
clrscr;
Write('Nhap n: ');readln(n);
chan:=0;le:=0;
Writeln('Nhap ',n,' phan tu cua mang:');
For i:= 1 to n do
Begin
write('A[',i,'] = ');
Readln(a[i]);
If a[i] mod 2 = 0 then
Begin
inc(chan);
daychan[chan]:=a[i];
end
else
Begin
inc(le);
dayle[le]:=a[i];
End;
End;
For i:= 1 to chan do
for j:= i to chan do If daychan[i]>daychan[j] then
Begin
tam:=daychan[i];
daychan[i]:=daychan[j];
daychan[j]:=tam;
End;
For i:= 1 to le do
for j:= i to le do If dayle[i]<dayle[j] then
Begin
tam:=dayle[i];
dayle[i]:=dayle[j];
dayle[j]:=tam
End;
Writeln('Day sau khi sap xep:');
For i:= 1 to chan do write(daychan[i],' ');
For i:= 1 to le do write(dayle[i],' ');
Readln;
End.
2:
#include <bits/stdc++.h>
using namespace std;
long long a[1000],i,n;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
sort(a+1,a+n+1);
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
for (i=n; i>=1; i--) cout<<a[i]<<" ";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
double a[100];
int i,n;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
for (i=1; i<=n; i++)
swap(a[i],a[n+1-i]);
for (i=1; i<=n; i++) cout<<a[i]<<" ";
return 0;
}
bn ơi bn làm bằng pascal đc ko ạ, mik cần cả hai cái lun
Bài 1:
#include <bits/stdc++.h>
using namespace std;
long long a[100],n,i,j,tam;
int main()
{
cin>>n;
for (i=1; i<=n; i++)
cin>>a[i];
for (i=1; i<=n-1; i++)
for (j=i+1; j<=n; j++)
if (a[i]<a[j]) swap(a[i],a[j]);
for (i=1; i<=n;i++)
cout<<a[i]<<" ";
return 0;
}