mramine364

move zeros

Aug 19th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.    
  7.     int t[] = { 1, 0, 12, 0, 4, 0, 0, 8 };
  8.     int n = 8, i=0;
  9.     while (i<n){
  10.         if (t[i] == 0){
  11.             int k = i + 1;
  12.             while (k < n && t[k] == 0)k++;
  13.             if (k == n) break;
  14.             for (int j = i; j < n - 1; j++)
  15.                 t[j] = t[j + k - i];
  16.             for (int j = 0; j < k - i; j++)
  17.                 t[n - 1 - j] = 0;          
  18.         }
  19.         i++;
  20.     }
  21.  
  22.     for (int j = 0; j < n; j++)
  23.         cout << t[j] << ", ";
  24.     cout << "\n";  
  25.  
  26.     getchar();
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment