#include <iostream>
using namespace std;
int main(){
int n, m=0, r;
cin>>n;
int arr[n], arrb[n];
for (int i=0; i<n; ++i) {
cin>>arr[i];
if(arr[i]%2==0) {
arrb[m]=i;
++m;
}
--m;
r=m;
for (int i=0; i<=m/2; ++i) {
int buf = arr[arrb[i]];
arr[arrb[i]]=arr[arrb[r]];
arr[arrb[r]]=buf;
--r;
for (int i=0; i<n; ++i) cout<<arr[i]<<" ";
Будут вопросы по решению - пиши.
int main() {
int n,k,m;
cout<<"n = "; cin>>n; k=n-1;
int a[n];
for (int i=0; i<n; i++) cin>>a[i];
for (int i=0; i<n; i++) {
if (a[i]%2==0) {
while ((a[k]%2!=0) && (k>i)) k--;
if (k>i) {
m=a[i]; a[i]=a[k]; a[k]=m; k--;
cout<<a[i]<<" ";
cout<<endl;
system("pause");
return 0;
n = 11
2 3 5 7 4 6 8 9 13 10 11
10 3 5 7 8 6 4 9 13 2 11
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
using namespace std;
int main(){
int n, m=0, r;
cin>>n;
int arr[n], arrb[n];
for (int i=0; i<n; ++i) {
cin>>arr[i];
if(arr[i]%2==0) {
arrb[m]=i;
++m;
}
}
--m;
r=m;
for (int i=0; i<=m/2; ++i) {
int buf = arr[arrb[i]];
arr[arrb[i]]=arr[arrb[r]];
arr[arrb[r]]=buf;
--r;
}
for (int i=0; i<n; ++i) cout<<arr[i]<<" ";
}
Будут вопросы по решению - пиши.
#include <iostream>
using namespace std;
int main() {
int n,k,m;
cout<<"n = "; cin>>n; k=n-1;
int a[n];
for (int i=0; i<n; i++) cin>>a[i];
for (int i=0; i<n; i++) {
if (a[i]%2==0) {
while ((a[k]%2!=0) && (k>i)) k--;
if (k>i) {
m=a[i]; a[i]=a[k]; a[k]=m; k--;
}
}
cout<<a[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}
n = 11
2 3 5 7 4 6 8 9 13 10 11
10 3 5 7 8 6 4 9 13 2 11