Viết chương trình nhập vào dãy a có n phần tử
A )trao đổi vị trí của giá trị max cho giá trị min
B )sắp xếp số lẻ ở đầu số chẵn ở cuố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.
#include <bits/stdc++.h>
using namespace std;
long long a[1000],i,n,t,dem;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
t=0;
dem=0;
for (i=1; i<=n; i++)
if ((a[i]>0) and (i%2==1))
{
t+=a[i];
dem++;
}
cout<<fixed<<setprecision(2)<<(t*1.0)/(dem*1.0);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
long long a[6],i,t,t1;
int main()
{
t=0;
for (i=1; i<=n; i++)
{
cin>>a[i];
if (a[i]%2==0) t=t+a[i];
}
t1=0;
for (i=1; i<=n; i++)
if ((a[i]%2!=0) and (i%2==0)) t1=t1+a[i];
cout<<t<<endl;
cout<<t1;
return 0;
}
uses crt;
var a:array[1..100]of integer;
i,n,dem,max,t,min,dem1:integer;
begin
clrscr;
write('n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
dem:=0;
max:=-32000;
for i:=1 to n do
begin
if a[i] mod 2=0 then
begin
dem:=dem+1;
if max<a[i] then max:=a[i];
end;
if dem=0 then writeln('Trong day khong co so chan')
else begin
writeln('So so chan la: ',dem);
writeln('So chan lon nhat la: ',max);
end;
t:=0;
for i:=1 to n do
if i mod 2=1 then t:=t+a[i];
writeln('Tong cac so o vi tri le la: ',t);
min:=maxint;
dem1:=0;
for i:=1 to n do
if a[i] mod 2<>0 then
begin
inc(dem1);
if min>a[i] then min:=a[i];
end;
if dem1=0 then writeln('Trong day khong co so le')
else writeln('So le nho nhat la: ',min);
readln;
end.
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.
Program HOC24;
var d,i,n: integer;
a: array[1..32000] of integer;
begin
write('Nhap N: '); readln(n);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
write('Cac phan tu cua mang vua nhap la: ');
for i:=1 to n do write(a[i],' ');
writeln;
d:=0;
for i:=1 to n do if 10 mod a[i]=0 then d:=d+1;
writeln('Co ',d,' phan tu co gia tri la uoc cua 10');
write('Cac phan tu o vi tri chan la: ');
for i:=1 to n do if i mod 2=0 then write(a[i],' ');
writeln;
write('Cac phan tu o vi tri le la: ');
for i:=1 to n do if i mod 2=1 then write(a[i],' ');
readln
end.
Bài 1:
uses crt;
var a:array[1..100]of integer;
i,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
for i:=1 to n do
if a[i] mod 2<>0 then write(a[i]:4);
readln;
end.
Bài 2:
uses crt;
var a:array[1..100]of integer;
i,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
for i:=1 to n do
if a[i] mod 2=0 then write(a[i]:4);
readln;
end.
program bai_2;
uses crt;
var a:array[1..100]of byte;
n,i,j,tam,vt,vt2,x,x2,k,ch:byte;
procedure yc1;
begin
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
for i:=1 to n do write(a[i]:3);
end;
procedure yc2;
begin
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
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('mang a:');for i:=1 to n do write(a[i]:3);
end;
procedure yc3;
begin
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
write('nhap so x:');readln(x);
for i:=n downto 1 do
if a[i]=x then vt:=i;if a[i]=x then writeln('vi tri cua ',x,' trong mang a la:',vt);writeln;
if a[i]<>x then writeln('khong tim thay ',x,' trong day so tren');writeln;
end;
procedure yc4;
begin
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
write('nhap so x2:');readln(x2);
for i:=n downto 1 do
if a[i]=x2 then vt2:=i;if x>0 then
begin
for i:=vt2 to n-1 do a[i]:=a[i+1];
for i:=1 to n-1 do write(a[i]:3);
end
else if a[i]<>x2 then write('khong tim thay ',x2,' trong day so tren');
end;
procedure yc5;
begin
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
write('nhap vi tri:');readln(vt);
for i:=1 to n do
if (vt=i) then writeln('phan tu o vi tri ',vt,' la:',a[vt]:3);
for i:=vt to n-1 do a[i]:=a[i+1];
for i:=n downto vt+1 do a[i]:=a[i-1];
write('nhap so can sua:');readln(a[vt]);
writeln('day so sau khi sua la:');
for i:=1 to n do write(a[i]:3); writeln;
end;
procedure yc6;
begin
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
write('nhap vi tri can chen k:');readln(k);
for i:=n+1 downto k+1 do a[i]:=a[i-1];
write('nhap so can chen vao day:');readln(a[k]);
writeln('day so sau khi chen ',a[k],' vao day la:');
for i:=1 to n+1 do write(a[i]:3);writeln;
end;
procedure yc7;
begin
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
for i:=1 to n do
write(a[i]:3);
writeln;
end;
BEGIN
clrscr;
while ch<8 do
begin
clrscr;
writeln('CHON MOT TRONG CAC SO SAU:');
writeln('1.NHAP DAY SO:');
writeln('2.SAP XEP DAY SO:');
writeln('3.TIM MOT SO:');
writeln('4.XOA MOT SO:');
writeln('5.SUA MOT SO:');
writeln('6.CHEN MOT SO:');
writeln('7.IN DAY SO:');
writeln('8.THOAT KHOI CHUONG TRINH:');
write('nhap so co yeu cau ban muon lam:');readln(ch);
if ch<8 then
begin write('nhap so n:');readln(n); end;
case ch of
1:yc1;
2:yc2;
3:yc3;
4:yc4;
5:yc5;
6:yc6;
7:yc7;
end;
readln;
end;
readln;
end.
#include <bits/stdc++.h>
using namespace std;
long long a[1000],i,n,nn;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++) if (a[i]%2!=0) cout<<i<<" ";
cout<<endl;
nn=a[1];
for (i=1; i<=n; i++) nn=min(nn,a[i]);
cout<<nn<<endl;
for (i=1; i<=n; i++) if (nn==a[i]) cout<<i<<" ";
return 0;
}
uses crt;
var a:array[1..50]of int64;
i,n,t,max,min:int64;
begin
clrscr;
readln(n);
for i:=1 to n do
readln(a[i]);
max:=a[1];
min:=a[1];
t:=0;
for i:=1 to n do
begin
if max<a[i] then max:=a[i];
if min>a[i] then min:=a[i];
t:=t+a[i];
end;
writeln('Tong la: ',t);
writeln('So lon nhat la: ',max);
write('Vi tri la: ');
for i:=1 to n do
if a[i]=max then write(i:4);
writeln;
writeln('So nho nhat la: ',min);
write('Vi tri la: ');
for i:=1 to n do
if a[i]=min then write(i:4);
readln;
end.
#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;
}
uses crt;
var n,i,tam,max,min:integer;
a:array[1..100]of integer;
begin
clrscr;
readln(n);
for i:=1 to n do readln(a[i]);
max:=a[1];
min:=a[1];
for i:=2 to n do
begin
if max<a[i] then max:=a[i];
if min>a[i] then min:=a[i];
end;
tam:=max;
max:=min;
min:=tam;
for i:=1 to n do write(a[i]:4);
writeln;
for i:=1 to n do
if a[i] mod 2<>0 then write(a[i]:4);
for i:=1 to n do
if a[i] mod 2=0 then write(a[i]:4);
readln;
end.
E lập trình trên pascal ạ