Problem p00031 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define PRINT(STR) cout << STR << endl;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int(i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) (V).begin(), (V).end()
#define SPACE " "

typedef long long ll;

using namespace std;

int nextInt() {
  int n = -1;
  cin >> n;
  return n;
}
double nextDouble() {
  double d = -1;
  cin >> d;
  return d;
}
ll nextll() {
  ll n = -1;
  cin >> n;
  return n;
}
string nextString() {
  string str;
  cin >> str;
  return str;
}

int INF = 1LL << 25;
double EPS = 1e-9;
int numofbits(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

// BEGIN //////////////////////////////////////////////////////////////
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    vector<int> ans;
    while (n) {
      for (int t = 1024;; t >>= 1) {
        if (n & t) {
          ans.push_back(t);
          n ^= t;
          break;
        }
      }
    }
    for (int i = ans.size() - 1; i; i--) {
      cout << ans[i] << SPACE;
    }
    cout << ans[0] << endl;
  }
  return 0;
}
// END   //////////////////////////////////////////////////////////////

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define PRINT(STR) cout << STR << endl;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int(i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) (V).begin(), (V).end()
#define SPACE " "

typedef long long ll;

using namespace std;

int nextInt() {
  int n = -1;
  cin >> n;
  return n;
}
double nextDouble() {
  double d = -1;
  cin >> d;
  return d;
}
ll nextll() {
  ll n = -1;
  cin >> n;
  return n;
}
string nextString() {
  string str;
  cin >> str;
  return str;
}

int INF = 1LL << 25;
double EPS = 1e-9;
int numofbits(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

// BEGIN //////////////////////////////////////////////////////////////
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    if (n == 0) {
      cout << 0 << endl;
      continue;
    }
    vector<int> ans;
    if (n % 2 == 1) {
      n -= 1;
      ans.push_back(1);
    }
    int t = 2;
    while (n) {
      if (numofbits(n) == 1) {
        ans.push_back(n);
        break;
      }
      n -= t;
      ans.push_back(t);
      t *= 2;
    }
    REP(i, ans.size() - 1) { cout << ans[i] << SPACE; }
    cout << ans[ans.size() - 1] << endl;
  }
  return 0;
}
// END   //////////////////////////////////////////////////////////////