Viết chương trình nhập một dãy số từ bàn phím, các số cách nhau bởi dấu cách, thực hiện sắp xếp dãy đã nhập theo một trong các thuật toán sắp xếp rồi in kết quả ra màn hình.
Viết chương trình nhập một dãy số gồm n số tự nhiên (n nhập từ bàn phím), sắp xếp chúng theo thứ tự tăng dần và in ra màn hình :
a) Dãy đã sắp
b) Các khoảng cách của hai số liền kề (hiệu của hai số đó )
#include <bits/stdc++.h>
using namespace std;
int A[1000],n,i;
int main()
{
cin>>n;
for (int i=1; i<=n; i++) cin>>A[i];
sort(A+1,A+n+1);
for (int i=1; i<=n; i++)
cout<<A[i]<<" ";
cout<<endl;
for (int i=2; i<=n; i++)
cout<<A[i]-A[i-1]<<endl;
return 0;
}
Viết một chương trình thực hiện các công việc sau:
- Nhập từ bàn phím 1 dãy A gồm N số nguyên (với N được nhập từ bàn phím).
- Sắp xếp dãy số đó theo chiều giảm dần.
- In ra mà hình tất cả các phần tử chẵn
- Tìm và đưa ra chỉ số của phần tử có gía trị = K (với K là số nguyên và được nhập từ bàn phím).
#include <bits/stdc++.h>
using namespace std;
long long n,i,a[1000];
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
sort(a+1,a+n+1);
for (i=n; i>=1; i--) cout<<a[i]<<" ";
return 0;
}
bài 1:Viết chương trình nhập vào một mảng số nguyên gồm n phần tử(n<100) nhập từ bàn phím thực hiện các công việc sau:
a)In ra màn hình các phần tử của mảng vừa nhập.
b) Sắp xếp dãy số nguyên theo thứ tự tăng dần.
c) Nhập số nguyên x bất kì.Tính số chắn lớn nhất không được vượt quá x.
Bài 1:
uses crt;
var a:array[1..100]of integer;
i,n,kt,max,x,j,tam:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
writeln('Mang ban vua nhap la: ');
for i:=1 to n do
write(a[i]:4);
writeln;
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;
writeln('Day tang dan la: ');
for i:=1 to n do
write(a[i]:4);
writeln;
write('Nhap x='); readln(x);
max:=0;
kt:=0;
for i:=1 to n do
if (a[i] mod 2=0) and (a[i]<=x) then
begin
if max<a[i] then max:=a[i];
kt:=1;
end;
if kt=0 then writeln('Trong day khong co so le')
else writeln('So chan lon nhat khong vuot qua ',x,' la: ',max);
readln;
end.
Em hãy viết chương trình nhập số nguyên n từ bàn phím sau đó nhập dãy số gồm n phần tử rồi thực hiện các yêu cầu sau:
- hiển thị dãy số vừa nhập ra màn hình theo hàng ngang, mỗi số cách nhau 4 dấu cách
- tính tổng số cách nhau 4 dấu cách
- tính tổng của dãy số
- tính số lớn nhất trong dãy số vừa nhập
- tìm số nhỏ nhất trong dãy số vừa nhập
- in ra màn hình các số lẻ của dãy số
- tính trung bình cộng của dãy số
giúp tui vs
Viết chương trình pascal nhập vào một mảng số nguyên B số lượng phần tử là m nhập từ bàn phím
+ sắp xếp mảng theo thứ tự giảm dần rồi in mảng đã sắp xếp ra màn hình
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.
1.Viết chương trình nhập vào một dãy số gồm n phần tử.Sắp xếp dãy số theo thứ tự tăng dần sau đó in ra màn hình dãy số đã sắp xếp. 2.Viết chương trình nhập vào một dãy số gồm n phần tử. Sắp xếp dãy số theo thứ tự giảm dần sau đó in ra màn hình dãy số đã sắp xếp.(cứu mk với mai thi r ạ🥲
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.
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ài 3. Viết chương trình nhập từ bàn phím dãy số thực có N phần tử (1<N<100). hãy in ra màn hình
a. dãy số ban đầu đã nhập
b. dãy số đã sắp xếp lại. biết sắp xếp các phần tử theo nguyên tắc sau: a1 đổi chỗ cho an, a2 đổi chỗ cho an-1,...
Giúp mik với ạ!!! Mik cần gấp!!!Mik xin cảm ơn trước ạ!!!
#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;
}
nhập vào từ bàn phím một dãy số nguyên(sử dụng chương trình con), tính tổng các phần tử đó, in ra màn hình( sử dụng chương trình con), sắp xếp dãy số nguyên thành dãy không giảm(sử dụng chương trình con)
#include <bits/stdc++.h>
using namespace std;
long long a[1000],i,n,t;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
t=0;
for (i=1; i<=n; i++) t+=a[i];
cout<<t<<endl;
sort(a+1,a+n+1);
for (i=1; i<=n; i++) cout<<a[i]<<" ";
return 0;
}
vieết chương trình nhập số tự nhiên n và dãy các số nguyên a1,a2,..,an từ bàn phím sắp xếp các số âm trong dãy theo thứ tự tăng , các số , còn lại giữ nguyên thứ, thông báo ra màn hình dãy sau khi đã sắp xếp . ràng buộc 0<=n<=10, |ai|<=10^4
ai giúp mình với ạ mình cần gấp lắm ạ