Problem p00220 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m;
  while (scanf("%d", &n), n >= 0) {
    string ans;
    char buff[8];

    if (getchar() == '.') {
      scanf("%s", buff);
      sscanf(buff, "%d", &m);
      for (int len = strlen(buff); len < 4; len++)
        m *= 10;
    } else
      m = 0;

    if (n > 255)
      ans = "NA";
    else {
      REP(i, 8) {
        if (n & (1 << i))
          ans = "1" + ans;
        else
          ans = "0" + ans;
      }

      ans += ".";

      REP(i, 4) {
        m *= 2;
        if (m >= 10000) {
          ans = ans + "1";
          m -= 10000;
        } else {
          ans = ans + "0";
        }
      }
      if (m != 0)
        ans = "NA";
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m;
  while (scanf("%d.%d", &n, &m), n >= 0) {
    string ans;

    if (n > 255)
      ans = "NA";
    else {
      REP(i, 8) {
        if (n & (1 << i))
          ans = "1" + ans;
        else
          ans = "0" + ans;
      }

      ans += ".";
      while (m * 10 < 10000)
        m *= 10;

      REP(i, 4) {
        m *= 2;
        if (m >= 10000) {
          ans = ans + "1";
          m -= 10000;
        } else {
          ans = ans + "0";
        }
      }
      if (m != 0)
        ans = "NA";

      cout << ans << endl;
    }
  }
  return 0;
}