Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void swap(int &from, int &to) {
- int _to = to;
- int _from = from;
- from = _to;
- to = _from;
- }
- int * sort(bool increase, int *m, int len) {
- int i;
- if (increase) {
- for (int j = 1; j < len; j++){
- if (m[j] < m[j - 1]) {
- i = j;
- do{
- swap(m[i], m[i - 1]);
- i--;
- } while (m[i] < m[i - 1] && i > 0);
- }
- }
- } else {
- for (int j = len; j > 0; j--){
- if (m[j] > m[j - 1]) {
- i = j;
- do{
- swap(m[i], m[i - 1]);
- i++;
- } while (m[i] > m[i - 1] && i < len);
- }
- }
- }
- return m;
- }
- int * increaseArray(int *m, int oldLen, int newLen) {
- int * newArr = new int[newLen];
- for (int i = 0; i < oldLen; i++)
- newArr[i] = m[i];
- delete[] m;
- return newArr;
- }
- int * increaseAndFill(int *m, int &n, int newValue) {
- m = increaseArray(m, n, n + 1);
- m[n] = newValue;
- n++;
- return m;
- }
- int main()
- {
- int n;
- cin >> n;
- int *m = new int[n];
- for (int i = 0; i < n; i++)
- cin >> m[i];
- int m5_n = 0;
- int *multiples_5 = new int[m5_n];
- for (int i = 0; i < n; i++) {
- if (m[i] % 5 == 0) {
- multiples_5 = increaseAndFill(multiples_5, m5_n, m[i]);
- m[i] = 999;
- }
- }
- int other_n = 0;
- int *other = new int[other_n];
- for (int i = 0; i < n; i++) {
- if (m[i] % 5 != 0 && m[i] != 999)
- other = increaseAndFill(other, other_n, m[i]);
- }
- sort(false, multiples_5, m5_n);
- sort(true, other, other_n);
- int m5_i = 0, other_i = 0;
- for (int i = 0; i < n; i++) {
- if (m[i] == 999) {
- m[i] = multiples_5[m5_i]; m5_i++;
- } else {
- m[i] = other[other_i]; other_i++;
- }
- }
- for (int i = 0; i < n; i++)
- cout << m[i] << " ";
- delete[] m;
- delete[] multiples_5;
- delete[] other;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement