alexjercan/bugnet huggingface dataset:
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int m, n[4], mi[4], ma[4], na;
  int max_int = 0, min_int = 0, n_int, ans;
  bool same;

  while (1) {
    ans = 0;
    same = false;

    scanf("%d", &n_int);
    if (n_int == 0)
      break;
    if (n_int % 1111 == 0) {
      cout << "NA" << endl;
      same = true;
    }

    for (int i = 3; i >= 0; i--) {
      n[i] = n_int / (int)(pow(10, i)) % 10;
    }

    while (n_int != 6174) {
      if (same)
        break;
      max_int = min_int = 0;

      for (int i = 3; i >= 0; i--) {
        n[i] = n_int / (int)(pow(10, i)) % 10;
      }
      sort(n, n + 4);

      for (int i = 0; i < 4; i++) {
        mi[3 - i] = n[i];
        ma[i] = n[i];
      }

      for (int i = 0; i < 4; i++) {
        max_int += ma[i] * (int)(pow(10, i));
        min_int += mi[i] * (int)(pow(10, i));
      }
      n_int = max_int - min_int;

      ans++;
    }

    if (!same)
      cout << ans << endl;
  }
  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int m, n[4], mi[4], ma[4], na;
  int max_int = 0, min_int = 0, n_int, ans;
  bool same = false;

  while (1) {
    ans = 0;

    scanf("%d", &n_int);

    for (int i = 3; i >= 0; i--) {
      n[i] = n_int / (int)(pow(10, i)) % 10;
    }

    na = n[0];
    for (int i = 1; i < 4; i++) {
      if (na != n[i])
        same = true;
    }
    if (!same && na == 0)
      break;
    if (!same)
      cout << "NA" << endl;

    while (n_int != 6174) {
      max_int = min_int = 0;
      if (!same)
        break;
      for (int i = 3; i >= 0; i--) {
        n[i] = n_int / (int)(pow(10, i)) % 10;
      }
      sort(n, n + 4);

      for (int i = 0; i < 4; i++) {
        mi[3 - i] = n[i];
        ma[i] = n[i];
      }

      for (int i = 0; i < 4; i++) {
        max_int += ma[i] * (int)(pow(10, i));
        min_int += mi[i] * (int)(pow(10, i));
      }
      n_int = max_int - min_int;

      ans++;
    }

    if (same)
      cout << ans << endl;
  }
  return (0);
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> n;
  int input;
  while (cin >> input) {
    int ans = 0;
    if (input == 0)
      break;
    if (input % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    while (input != 6174) {
      for (int i = 0; i < 4; i++) {
        n.push_back(input % 10);
        input /= 10;
      }
      vector<int> minNum, maxNum;

      sort(n.begin(), n.end());
      minNum = n;
      reverse(n.begin(), n.end());
      maxNum = n;

      int L = 0, S = 0;
      for (int i = 0, m = 1000; i < 4; i++, m /= 10) {
        L += maxNum[i] * m;
        S += minNum[i] * m;
      }
      input = L - S;
      ans++;

      n.clear();
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
using namespace std;

int main() {
  char input[4];
  char array[4];
  int n, l, s;
  int count;

  while (cin >> input, atoi(input)) {
    count = 0;
    n = atoi(input);
    if (atoi(input) % 1111 == 0) {
      cout << "NA" << endl;
    } else {
      while (n != 6174) {
        sprintf(input, "%d", n);
        sort(input, input + 4);
        s = atoi(input);
        sort(input, input + 4, greater<int>());
        l = atoi(input);
        n = l - s;
        count++;
      }
      cout << count << endl;
    }
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

string to_string_keta(const long long int l, const int keta) {
  ostringstream oss;

  oss << std::setw(keta) << std::setfill('0') << l;

  return oss.str();
}

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    if (N % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int num = 0;
    while (1) {
      if (N == 6174)
        break;
      string st(to_string_keta(N, 4));
      sort(st.begin(), st.end());
      int amin(stoi(st));
      reverse(st.begin(), st.end());
      int amax(stoi(st));
      N = amax - amin;
      num++;
    }
    cout << num << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

string to_string_keta(const long long int l, const int keta) {
  ostringstream oss;

  oss << setprecision(keta) << l;

  return oss.str();
}

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    if (N % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int num = 0;
    while (1) {
      if (N == 6174)
        break;
      string st(to_string_keta(N, 4));
      sort(st.begin(), st.end());
      int amin(stoi(st));
      reverse(st.begin(), st.end());
      int amax(stoi(st));
      N = amax - amin;
      num++;
    }
    cout << num << endl;
  }

  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
using namespace std;

int to[10000];
int memo[10000];

void init() {
  int L, S;
  char rstr[8];
  char str[8];
  rep(i, 10000) memo[i] = -1;
  rep(i, 10000) {
    sprintf(rstr, "%04d", i);
    sprintf(str, "%04d", i);
    sort(rstr, rstr + 4);
    sort(str, str + 4);
    reverse(str, str + 4);
    L = atoi(str);
    S = atoi(rstr);
    to[i] = L - S;
  }
}

int main(void) {
  int n;
  init();

  while (scanf("%d", &n), n) {
    int ans = 0;
    int n_ = n;
    if (to[n] == 0) {
      puts("NA");
      continue;
    }
    if (memo[n] != -1) {
      ans = memo[n];
    } else {
      while (n != 6174) {
        n = to[n];
        ans++;
      }
      memo[n_] = ans;
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
using namespace std;

int to[10000];
int memo[10000];

void init() {
  string str, rstr;
  int L, S;
  stringstream ss;
  rep(i, 10000) memo[i] = -1;
  rep(i, 10000) {
    ss.clear();
    ss << i;
    ss >> rstr;
    sort(rstr.begin(), rstr.end());
    str = rstr;
    reverse(str.begin(), str.end());
    ss.clear();
    ss << (str + " " + rstr);
    ss >> L >> S;
    to[i] = L - S;
  }
}

int main(void) {
  int n;
  init();

  while (scanf("%d", &n), n) {
    int ans = 0;
    int n_ = n;
    if (n == 0)
      break;
    if (to[n] == 0) {
      puts("NA");
      continue;
    }
    if (memo[n] != -1) {
      ans = memo[n];
    } else {
      while (n != 6174) {
        n = to[n];
        ans++;
      }
      memo[n_] = ans;
    }
    printf("%d\\n", ans);
  }

  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int latte(string str) {
  stringstream ss(str);
  int n;
  ss >> n;
  return n;
}
struct data {
  string str;
  int n;
  data(string a, int b) : str(a), n(b) {}
  data() {}
};
int main() {
  string str = "";
  while (cin >> str, str != "0000") {
    int memo[10000];
    fill(memo, memo + 10000, -1);
    queue<data> Q;
    Q.push(data(str, 0));
    while (!Q.empty()) {
      data ins = Q.front();
      Q.pop();
      str = ins.str;
      int n = ins.n;
      if (memo[latte(str)] != -1)
        continue;
      memo[latte(str)] = n;
      string a, b;
      a = b = str;
      sort(a.rbegin(), a.rend());
      sort(b.begin(), b.end());
      int to = latte(a) - latte(b);
      stringstream ss;
      ss << to;
      string to_;
      ss >> setw(4) >> setfill('0') >> to_;
      if (to_.length() != 4)
        for (int i = 0; i < 4 - to_.length(); i++)
          to_ = "0" + to_;
      // cout<<a<<"-"<<b<<"="<<to_<<endl;
      Q.push(data(to_, n + 1));
    }
    if (memo[6174] == -1)
      cout << "NA" << endl;
    else
      cout << memo[6174] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int cnv(string str, bool gFlag) {
  sort(str.begin(), str.end());
  if (gFlag)
    reverse(str.begin(), str.end());
  stringstream ss(str);
  int n;
  ss >> n;
  return n;
}
int main() {
  string str;
  while (cin >> str, str != "0000") {
    bool flag = false;
    for (int i = 1; i < 4; i++)
      if (str[0] != str[i])
        flag = true;

    if (!flag) {
      cout << "NA" << endl;
      continue;
    }
    int cnt = 0;
    while (str != "6174") {
      int n = cnv(str, true) - cnv(str, false);
      stringstream ss;
      ss << n;
      ss >> str;
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>

int main() {
  int n;
  bool used[10000];
  while (std::cin >> n) {
    int str[4] = {(n / 1000), (n / 100) % 10, (n / 10) % 10, n % 10};
    if (n % 1111 == 0) {
      if (str[0] == 0)
        break;
      else
        std::cout << "NA" << std::endl;
    }
    std::memset(used, false, sizeof(used));
    used[n] = true;
    int cnt = 0;
    while (n != 6174) {
      ++cnt;
      std::sort(str, str + 4, std::greater<int>());
      int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
      std::sort(str, str + 4);
      int s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
      n = l - s;
      if (used[n])
        goto a;
      used[n] = true;
      str[0] = n / 1000;
      str[1] = (n / 100) % 10;
      str[2] = (n / 10) % 10;
      str[3] = n % 10;
    }
    std::cout << cnt << std::endl;
  a:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>

int main() {
  int n;
  int str[4];
  while (std::cin >> n) {
    int str[4] = {(n / 1000), (n / 100) % 10, (n / 10) % 10, n % 10};
    if (str[0] == str[1] == str[2] == str[3]) {
      if (str[0] == 0)
        break;
      else
        std::cout << "NA" << std::endl;
    }
    int cnt = 0;
    while (n != 6174) {
      if (str[0] == str[1] == str[2] == str[3])
        std::cout << "NA" << std::endl;
      ++cnt;
      std::sort(str, str + 4, std::greater<int>());
      int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
      std::sort(str, str + 4);
      int s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
      n = l - s;
      str[0] = n / 1000;
      str[1] = (n / 100) % 10;
      str[2] = (n / 10) % 10;
      str[3] = n % 10;
    }
    std::cout << cnt << std::endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, c[4];
  while ((cin >> n), n) {
    if (n % 1111 == 0) {
      cout << "NA\\n";
      continue;
    }
    int num = 0;
    while (n != 6174) {
      for (int i = 0; i < 4; i++) {
        c[i] = n % 10;
        n /= 10;
      }
      sort(c, c + 4);
      int p = 0, q = 0;
      for (int i = 0; i < 4; i++) {
        p *= 10;
        q *= 10;
        p += c[i];
        q += c[3 - i];
      }
      n = q - p;
      num++;
    }
    cout << num << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int check(int *a) {
  vector<int> b;
  for (int i = 0; i < 4; i++) {
    b.push_back(*a % 10);
    *a /= 10;
  }
  sort(b.begin(), b.end());
  int p = 0, q = 0, u = 1;
  for (int i = 0; i < 4; i++) {
    q += b[i] * u;
    p += b[3 - i] * u;
    u *= 10;
  }
  return q - p;
}

int main() {
  int t;
  while (true) {
    cin >> t;
    if (t == 0)
      break;
    bool f = true;
    for (int i = 1; i < 9; i++)
      if (t == i * 1111) {
        cout << "NA\\n";
        f = false;
      }
    if (f) {
      if (t == 6174)
        cout << 0 << endl;
      else {
        int kk = 1;
        int pp = check(&t);
        while (pp != 6174) {
          pp = check(&pp);
          kk++;
        }
        cout << kk << endl;
      }
    }
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int num;
    cin >> num;
    if (num == 0)
      break;
    int numnum[4];
    int ans = 0;
    while (1) {
      if (num == 6174) {
        cout << ans << endl;
        break;
      }
      numnum[0] = num % 10000 / 1000;
      numnum[1] = num % 1000 / 100;
      numnum[2] = num % 100 / 10;
      numnum[3] = num % 10 / 1;
      if (numnum[0] == numnum[1] && numnum[0] == numnum[2] &&
          numnum[0] == numnum[3]) {
        puts("NA");
        break;
      }
      int nummax[4];
      int nummin[4];
      for (int i = 0; i < 4; i++) {
        nummax[i] = numnum[i];
        nummin[i] = numnum[i];
      }
      sort(nummax, nummax + 4);
      sort(nummin, nummin + 4);
      swap(nummax[0], nummax[3]);
      swap(nummax[1], nummax[2]);
      int mx =
          nummax[0] * 1000 + nummax[1] * 100 + nummax[2] * 10 + nummax[3] * 1;
      int mn =
          nummin[0] * 1000 + nummin[1] * 100 + nummin[2] * 10 + nummin[3] * 1;
      // printf("[%d][%d]\\n",mx,mn);
      num = mx - mn;
      ans++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int num;
    cin >> num;
    if (num == 0)
      break;
    int numnum[4];
    int ans = 0;
    while (1) {
      if (num == 6174) {
        cout << ans << endl;
        break;
      }
      numnum[0] = num % 10000 / 1000;
      numnum[1] = num % 1000 / 100;
      numnum[2] = num % 100 / 10;
      numnum[3] = num % 10 / 1;
      if (numnum[0] == numnum[1] && numnum[0] == numnum[2] &&
          numnum[0] == numnum[3])
        puts("NA");
      int nummax[4];
      int nummin[4];
      for (int i = 0; i < 4; i++) {
        nummax[i] = numnum[i];
        nummin[i] = numnum[i];
      }
      sort(nummax, nummax + 4);
      sort(nummin, nummin + 4);
      swap(nummax[0], nummax[3]);
      swap(nummax[1], nummax[2]);
      int mx =
          nummax[0] * 1000 + nummax[1] * 100 + nummax[2] * 10 + nummax[3] * 1;
      int mn =
          nummin[0] * 1000 + nummin[1] * 100 + nummin[2] * 10 + nummin[3] * 1;
      // printf("[%d][%d]\\n",mx,mn);
      num = mx - mn;
      ans++;
    }
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

string toString(int n) {
  string ret = "";
  while (n) {
    ret += (n % 10) + '0';
    n /= 10;
  }
  while (ret.size() < 4)
    ret += "0";
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  string s;

  while (cin >> s) {
    if (s == "0000")
      break;

    if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (s != "6174") {
      string L = s, S = s;
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      int nex = atoi(L.c_str()) - atoi(S.c_str());
      s = toString(nex);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

string toString(int n) {
  string ret = "";
  while (n) {
    ret += (n % 10) + '0';
    n /= 10;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  string s;

  while (cin >> s) {
    if (s == "0000")
      break;

    if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (s != "6174") {
      string L = s, S = s;
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      int nex = atoi(L.c_str()) - atoi(S.c_str());
      s = toString(nex);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  string s;
  int l[4], r[4], m[4];
  while (1) {
    cin >> s;
    if (s == "0000")
      break;
    if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
      cout << "NA" << endl;
      continue;
    }
    m[0] = s[0] - '0', m[1] = s[1] - '0', m[2] = s[2] - '0', m[3] = s[3] - '0';
    for (int i = 0;; ++i) {
      if (m[0] == 6 && m[1] == 1 && m[2] == 7 && m[3] == 4) {
        cout << i << endl;
        break;
      }
      l[0] = m[0], l[1] = m[1], l[2] = m[2], l[3] = m[3];
      sort(l, l + 4);
      r[0] = l[0], r[1] = l[1], r[2] = l[2], r[3] = l[3];
      reverse(r, r + 4);
      int k = r[0] * 1000 + r[1] * 100 + r[2] * 10 + r[3] - l[0] * 1000 -
              l[1] * 100 - l[2] * 10 - l[3];
      m[0] = k / 1000, k -= m[0] * 1000;
      m[1] = k / 100, k -= m[1] * 100;
      m[2] = k / 10, k -= m[2] * 10;
      m[3] = k;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

string int_to_string(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}

int string_to_int(const string &s) {
  int n;
  istringstream iss(s);
  iss >> n;
  return n;
}

bool is_equal(const string &s) {
  for (int i = 1; i < s.size(); ++i) {
    if (s[0] != s[i])
      return false;
  }
  return true;
}

int main() {
  int cnt;
  string s, max_, min_;
  while (1) {
    cin >> s;
    if (s == "0000")
      break;
    if (is_equal(s)) {
      cout << "NA" << endl;
      continue;
    }
    for (cnt = 0; s != "6174"; ++cnt) {
      min_ = s;
      sort(min_.begin(), min_.end());
      max_ = min_;
      reverse(max_.begin(), max_.end());
      s = int_to_string(string_to_int(max_) - string_to_int(min_));
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int cnt, a, b;
  char num[5];
  while (1) {
    cin >> num;
    cnt = 0;
    if (strcmp(num, "0000") == 0)
      break;
    if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) {
      cout << "NA" << endl;
      continue;
    }
    while (1) {
      if (strcmp(num, "6174") == 0)
        break;
      cnt++;
      sort(num, num + 4);
      a = atoi(num);
      sort(num, num + 4, greater<char>());
      b = atoi(num);
      b -= a;
      sprintf(num, "%04d", b);
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int cnt, a, b;
  char num[5];
  while (1) {
    cin >> num;
    cnt = 0;
    if (strcmp(num, "0000") == 0)
      break;
    if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) {
      cout << "NA" << endl;
      continue;
    }
    while (1) {
      if (strcmp(num, "6174") == 0)
        break;
      cnt++;
      sort(num, num + 4);
      a = atoi(num);
      sort(num, num + 4, greater<char>());
      b = atoi(num);
      b -= a;
      sprintf(num, "%d", b);
    }
    cout << cnt << endl;
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main(void) {
  int n;
  int l, s;
  int i, j, temp;
  int keta[4];
  int cnt;

  scanf("%d", &n);

  while (n != 0000) {
    if (n % 1111 == 0) {
      printf("NA\\n");
    } else {
      cnt = 0;
      while (n != 6174) {
        for (i = 0; i < 4; i++) {
          keta[i] = n % 10;
          n = n / 10;
        }
        for (i = 0; i < 3; i++) {
          for (j = i + 1; j < 4; j++) {
            if (keta[i] < keta[j]) {
              temp = keta[i];
              keta[i] = keta[j];
              keta[j] = temp;
            }
          }
        }
        l = keta[0] * 1000 + keta[1] * 100 + keta[2] * 10 + keta[3];

        for (i = 0; i < 3; i++) {
          for (j = i + 1; j < 4; j++) {
            if (keta[i] > keta[j]) {
              temp = keta[i];
              keta[i] = keta[j];
              keta[j] = temp;
            }
          }
        }
        s = keta[0] * 1000 + keta[1] * 100 + keta[2] * 10 + keta[3];
        n = l - s;
        cnt++;
      }
      printf("%d\\n", cnt);
    }
    scanf("%d", &n);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main(void) {
  int n;
  int l, s;
  int i, j, temp;
  int keta[4];
  int cnt;

  scanf("%d", &n);

  while (n != 0000) {
    if (n % 1111 == 0) {
      printf("NA\\n");
      scanf("%d", &n);
    }
    cnt = 0;
    while (n != 6174) {
      for (i = 0; i < 4; i++) {
        keta[i] = n % 10;
        n = n / 10;
      }
      for (i = 0; i < 3; i++) {
        for (j = i + 1; j < 4; j++) {
          if (keta[i] < keta[j]) {
            temp = keta[i];
            keta[i] = keta[j];
            keta[j] = temp;
          }
        }
      }
      l = keta[0] * 1000 + keta[1] * 100 + keta[2] * 10 + keta[3];

      for (i = 0; i < 3; i++) {
        for (j = i + 1; j < 4; j++) {
          if (keta[i] > keta[j]) {
            temp = keta[i];
            keta[i] = keta[j];
            keta[j] = temp;
          }
        }
      }
      s = keta[0] * 1000 + keta[1] * 100 + keta[2] * 10 + keta[3];
      n = l - s;
      cnt++;
    }
    printf("%d\\n", cnt);
    scanf("%d", &n);
  }
  return 0;
}