Sort the array from small to large directly by sorting, and move the first number that is not 0 after sorting to the first place.
#include <iostream> #include <bits/stdc++.h> using namespace std; intmain() { int a[10001]; int k = 0; for(int i = 0; i < 10; i++) { int number; cin >> number; while(number) { a[k++] = i; number--; } } sort(a, a + k); for(int i = 0; i < k; i++) { if(a[i] != 0) { int temp = a[i]; a[i] = a[0]; a[0] = temp; break; } } for(int i = 0; i < k; i++) { cout << a[i]; } return 0; }