Advertisement
kxcoze

satie_request

Apr 3rd, 2020
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <iomanip>
  5.  
  6. using namespace std;                                                                                                                                                                                                                                                            
  7.                                                                                                                                                                                                                                                                                
  8. void printArr(vector<float> &x) {                                                                                                                                                                                                                                              
  9.     for (float i : x)                                                                                                                                                                                                                                                          
  10.         cout << setprecision(2) << i << ' ';                                                                                                                                                                                                                                    
  11.     cout << '\n';                                                                                                                                                                                                                                                              
  12. }                                                                                                                                                                                                                                                                              
  13.                                                                                                                                                                                                                                                                                
  14. void sort_shit(vector<float> &x) {                                                                                                                                                                                                                                              
  15.     for (int i = 0; i < x.size(); i++) {                                                                                                                                                                                                                                        
  16.         if (abs(x[i]) < 1) {                                                                                                                                                                                                                                                    
  17.             int j = i;                                                                                                                                                                                                                                                          
  18.             while (x[j--] < 1) {                                                                                                                                                                                                                                                
  19.                 swap(x[j], x[j+1]);                                                                                                                                                                                                                                            
  20.                 if (j == 0 || abs(x[j-1]) < 1)                                                                                                                                                                                                                                  
  21.                     break;                                                                                                                                                                                                                                                      
  22.             }                                                                                                                                                                                                                                                                  
  23.         }                                                                                                                                                                                                                                                                      
  24.     }                                                                                                                                                                                                                                                                          
  25. }                                                                                                                                                                                                                                                                              
  26.                                                                                                                                                                                                                                                                                
  27. int main() {                                                                                                                                                                                                                                                                    
  28.     int n;                                                                                                                                                                                                                                                                      
  29.     cin >> n;                                                                                                                                                                                                                                                                  
  30.     vector<float> v(n);                                                                                                                                                                                                                                                        
  31.     for (int i = 0; i < n; i++) {                                                                                                                                                                                                                                              
  32.         v[i] = (float)rand() / RAND_MAX * 4 - 2;                                                                                                                                                                                                                                
  33.     }                                                                                                                                                                                                                                                                          
  34.                                                                                                                                                                                                                                                                                
  35.     printArr(v);                                                                                                                                                                                                                                                                
  36.     sort_shit(v);                                                                                                                                                                                                                                                              
  37.     printArr(v);                                                                                                                                                                                                                                                                
  38.     return 0;                                                                                                                                                                                                                                                                  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement