Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void swap(int &from, int &to) {
  6.     int _to = to;
  7.     int _from = from;
  8.     from = _to;
  9.     to = _from;
  10. }
  11.  
  12. int * sort(bool increase, int *m, int len) {
  13.     int i;
  14.     if (increase) {
  15.         for (int j = 1; j < len; j++){
  16.             if (m[j] < m[j - 1]) {
  17.                 i = j;
  18.                 do{
  19.                     swap(m[i], m[i - 1]);
  20.                     i--;
  21.                 } while (m[i] < m[i - 1] && i > 0);
  22.             }
  23.         }
  24.     } else {
  25.         for (int j = len; j > 0; j--){
  26.             if (m[j] > m[j - 1]) {
  27.                 i = j;
  28.                 do{
  29.                     swap(m[i], m[i - 1]);
  30.                     i++;
  31.                 } while (m[i] > m[i - 1] && i < len);
  32.             }
  33.         }
  34.     }
  35.     return m;
  36. }
  37.  
  38. int * increaseArray(int *m, int oldLen, int newLen) {
  39.     int * newArr = new int[newLen];
  40.     for (int i = 0; i < oldLen; i++)
  41.         newArr[i] = m[i];
  42.     delete[] m;
  43.     return newArr;
  44. }
  45.  
  46. int * increaseAndFill(int *m, int &n, int newValue) {
  47.     m = increaseArray(m, n, n + 1);
  48.     m[n] = newValue;
  49.     n++;
  50.     return m;
  51. }
  52.  
  53. int main()
  54. {
  55.     int n;
  56.     cin >> n;
  57.  
  58.     int *m = new int[n];
  59.     for (int i = 0; i < n; i++)
  60.         cin >> m[i];
  61.  
  62.     int m5_n = 0;
  63.     int *multiples_5 = new int[m5_n];
  64.  
  65.     for (int i = 0; i < n; i++) {
  66.         if (m[i] % 5 == 0) {
  67.             multiples_5 = increaseAndFill(multiples_5, m5_n, m[i]);
  68.             m[i] = 999;
  69.         }
  70.     }
  71.  
  72.     int other_n = 0;
  73.     int *other = new int[other_n];
  74.  
  75.     for (int i = 0; i < n; i++) {
  76.         if (m[i] % 5 != 0 && m[i] != 999)
  77.             other = increaseAndFill(other, other_n, m[i]);
  78.     }
  79.  
  80.     sort(false, multiples_5, m5_n);
  81.     sort(true, other, other_n);
  82.    
  83.     int m5_i = 0, other_i = 0;
  84.     for (int i = 0; i < n; i++) {
  85.         if (m[i] == 999) {
  86.             m[i] = multiples_5[m5_i]; m5_i++;
  87.         } else {
  88.             m[i] = other[other_i]; other_i++;
  89.         }
  90.     }
  91.  
  92.     for (int i = 0; i < n; i++)
  93.         cout << m[i] << " ";
  94.  
  95.     delete[] m;
  96.     delete[] multiples_5;
  97.     delete[] other;
  98.  
  99.     system("pause");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement