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

#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int floor_num(int n) {
  int ret = 0, i;
  if (n == 0) {
    return 1;
  } else if (n < 0) {
    ret++;
  }
  for (i = 0; n != 0; i++) {
    n /= 10;
  }
  ret += i;
  return ret;
}

char *int_to_str(int n) {
  int i, tmp, len;
  char *str;
  if (n == 0) {
    str = (char *)malloc(sizeof(char) * 2);
    strcpy(str, "0");
  }
  len = floor_num(n) + 1;
  str = (char *)malloc(sizeof(char) * len);
  str[len - 1] = '\\0';
  if (n < 0) {
    n = -n;
    str[0] = '-';
    for (i = len - 2; i > 0; i--) {
      str[i] = '0' + n % 10;
      n /= 10;
    }
  } else {
    for (i = len - 2; i >= 0; i--) {
      str[i] = '0' + n % 10;
      n /= 10;
    }
  }
  return str;
}

bool mirror_judge(int n) {
  int len, i;
  char *str;
  str = int_to_str(n);
  len = strlen(str);
  if (len % 2 == 0) {
    for (i = 0; i < len / 2; i++) {
      if (str[len / 2 + i] != str[len / 2 - 1 - i]) {
        break;
      }
    }
    if (i == len / 2) {
      return true;
    }
  } else {
    for (i = 0; i < len / 2; i++) {
      if (str[len / 2 + 1 + i] != str[len / 2 - 1 - i]) {
        break;
      }
    }
    if (i == len / 2) {
      return true;
    }
  }
  return false;
}

int main(void) {
  int i, j, n, ans, a1, a2;
  scanf("%d", &n);
  for (i = n;; i++) {
    if (mirror_judge(i)) {
      break;
    }
  }
  a1 = i;
  for (i = n;; i--) {
    if (mirror_judge(i)) {
      break;
    }
  }
  a2 = i;
  if (a1 - n >= n - a2) {
    ans = a2;
  } else {
    ans = a1;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int floor_num(int n) {
  int ret = 0, i;
  if (n == 0) {
    return 1;
  } else if (n < 0) {
    ret++;
  }
  for (i = 0; n != 0; i++) {
    n /= 10;
  }
  ret += i;
  return ret;
}

char *int_to_str(int n) {
  int i, tmp, len;
  char *str;
  if (n == 0) {
    str = (char *)malloc(sizeof(char) * 2);
    strcpy(str, "0");
  }
  len = floor_num(n) + 1;
  str = (char *)malloc(sizeof(char) * len);
  str[len - 1] = '\\0';
  if (n < 0) {
    n = -n;
    str[0] = '-';
    for (i = len - 2; i > 0; i--) {
      str[i] = '0' + n % 10;
      n /= 10;
    }
  } else {
    for (i = len - 2; i >= 0; i--) {
      str[i] = '0' + n % 10;
      n /= 10;
    }
  }
  return str;
}

bool mirror_judge(int n) {
  int len, i;
  char *str;
  str = int_to_str(n);
  len = strlen(str);
  if (len % 2 == 0) {
    for (i = 0; i < len / 2; i++) {
      if (str[len / 2 + i] != str[len / 2 - 1 - i]) {
        break;
      }
    }
    if (i == len / 2) {
      return true;
    }
  } else {
    for (i = 0; i < len / 2; i++) {
      if (str[len / 2 + 1 + i] != str[len / 2 - 1 - i]) {
        break;
      }
    }
    if (i == len / 2) {
      return true;
    }
  }
  return false;
}

int main(void) {
  int i, j, n, ans, a1, a2;
  scanf("%d", &n);
  for (i = n;; i++) {
    if (mirror_judge(i)) {
      break;
    }
  }
  a1 = i;
  for (i = n;; i--) {
    if (mirror_judge(i)) {
      break;
    }
  }
  a2 = i;
  if (a1 - n >= n - a2) {
    ans = a2;
  } else {
    ans = a1;
  }
  printf("%d\\n", ans);
  while (1)
    ;
  return 0;
}