#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int arr[n], k=0;
for (int i=0; i<n; ++i) {cin>>arr[i]; if (arr[i]==0) ++k;}
for (int i=0; i<n-1; ++i) {
for (int j=0; j<n-i-1; ++j) {
if (arr[j]>arr[j+1]) {
int temp;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
if (k>0) {
int m;
for (m=0; m<n; ++m) if (arr[m]==0) break;
for (int i=m; i<n-k; ++i) arr[i]=arr[i+k];
for (int i=n-k; i<n; ++i) arr[i]=0;
for (int i=0; i<n; ++i) cout<<arr[i]<<" ";
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int arr[n], k=0;
for (int i=0; i<n; ++i) {cin>>arr[i]; if (arr[i]==0) ++k;}
for (int i=0; i<n-1; ++i) {
for (int j=0; j<n-i-1; ++j) {
if (arr[j]>arr[j+1]) {
int temp;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
if (k>0) {
int m;
for (m=0; m<n; ++m) if (arr[m]==0) break;
for (int i=m; i<n-k; ++i) arr[i]=arr[i+k];
for (int i=n-k; i<n; ++i) arr[i]=0;
}
for (int i=0; i<n; ++i) cout<<arr[i]<<" ";
return 0;
}