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

#include <stdio.h>

int main() {

  char ty;
  char c1, c2, c3, c4, ch5;
  int n1 = 0, n2 = 0, n3 = 0, n4 = 0, N = 0, S = 0, P = 0, K = 0;
  int n[5] = {};
  int F = 0;
  while (1) {
    // n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';

    bool f = false;
    bool FF = false;
    do {
      scanf("%c%c%c%c%c", &c1, &c2, &c3, &c4, &ch5);
      ty = c1;

      if (ty == c1 && ty == c3 && ty == c4 && ty == c2)
        f = false;
      else
        f = true;

      if (ty == '0')
        f = true;
      if (f == false)
        printf("NA\\n");

    } while (f != true);
    if (c1 == '0' && c2 == '0' && c3 == '0' && c4 == '0')
      break;
    F++;
    if (F == 10001)
      break;

    if (c1 == '6' && c2 == '1' && c3 == '7' && c4 == '4')
      FF = true;

    for (int i = 0; i < 4; i++)
      n[i] = 0;
    int cnt = 0;

    n[0] = c1 - '0';
    n[1] = c2 - '0';
    n[2] = c3 - '0';
    n[3] = c4 - '0';

    for (int i = 0; i < 3; i++)
      for (int j = 3; j > i; j--)
        if (n[j] > n[j - 1]) {
          int t = n[j];
          n[j] = n[j - 1];
          n[j - 1] = t;
        }

    N = 1000 * n[0] + 100 * n[1] + 10 * n[2] + n[3];
    S = 1000 * n[3] + 100 * n[2] + 10 * n[1] + n[0];

    P = N - S;
    if (FF != true)
      cnt++;

    while (P != 6174) {
      N = P;
      K = P;
      n[0] = N / 1000;
      K = N % 1000;
      n[1] = K / 100;
      K = N % 100;
      n[2] = K / 10;
      K = N % 10;
      n[3] = K;

      // printf(" %d\\n",N);

      for (int i = 0; i < 3; i++)
        for (int j = 3; j > i; j--)
          if (n[j] > n[j - 1]) {
            int t = n[j];
            n[j] = n[j - 1];
            n[j - 1] = t;
          }

      N = 1000 * n[0] + 100 * n[1] + 10 * n[2] + n[3];
      S = 1000 * n[3] + 100 * n[2] + 10 * n[1] + n[0];

      cnt++;
      P = N - S;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {

  char c1, c2, c3, c4, ch5;
  int n1 = 0, n2 = 0, n3 = 0, n4 = 0, N = 0, S = 0, P = 0, K = 0;
  int n[5] = {};

  while (scanf("%c%c%c%c%c", &c1, &c2, &c3, &c4, &ch5) != EOF) {
    // n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
    for (int i = 0; i < 4; i++)
      n[i] = 0;
    int cnt = 0;

    n[0] = c1 - '0';
    n[1] = c2 - '0';
    n[2] = c3 - '0';
    n[3] = c4 - '0';

    for (int i = 0; i < 3; i++)
      for (int j = 3; j > i; j--)
        if (n[j] > n[j - 1]) {
          int t = n[j];
          n[j] = n[j - 1];
          n[j - 1] = t;
        }

    N = 1000 * n[0] + 100 * n[1] + 10 * n[2] + n[3];
    S = 1000 * n[3] + 100 * n[2] + 10 * n[1] + n[0];

    P = N - S;
    if (P != 6174)
      cnt++;

    while (P != 6174) {
      N = P;
      K = P;
      n[0] = N / 1000;
      K = N % 1000;
      n[1] = K / 100;
      K = N % 100;
      n[2] = K / 10;
      K = N % 10;
      n[3] = K;

      // printf(" %d\\n",N);

      for (int i = 0; i < 3; i++)
        for (int j = 3; j > i; j--)
          if (n[j] > n[j - 1]) {
            int t = n[j];
            n[j] = n[j - 1];
            n[j - 1] = t;
          }

      N = 1000 * n[0] + 100 * n[1] + 10 * n[2] + n[3];
      S = 1000 * n[3] + 100 * n[2] + 10 * n[1] + n[0];

      cnt++;
      P = N - S;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}