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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const string END = "6174";

bool isNG(string s) {
  REP(i, s.length()) if (s[0] != s[i]) return false;
  return true;
}

int make_L(string N) {
  SORT(N);
  REVERSE(N);
  int ret = 0;
  REP(i, N.length()) {
    ret *= 10;
    ret += (N[i] - '0');
  }
  return ret;
}

int make_S(string N) {
  SORT(N);
  int ret = 0;
  REP(i, N.length()) {
    ret *= 10;
    ret += (N[i] - '0');
  }
  return ret;
}

int main() {
  string N;
  while (cin >> N && N != "0000") {
    int cnt = 0;
    if (isNG(N)) {
      cout << "NA" << endl;
      continue;
    }
    while (N != END) {
      // REP(i, 10){
      int L = make_L(N);
      int S = make_S(N);
      // cout <<L <<", " <<S <<endl;
      stringstream ss;
      ss << (L - S);
      string s = ss.str();
      while (s.length() < 4)
        s = "0" + s;
      // cout <<s <<endl;
      N = s;
      ++cnt;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const string END = "6174";

bool isNG(string s) {
  REP(i, s.length()) if (s[0] != s[i]) return false;
  return true;
}

int make_L(string N) {
  SORT(N);
  REVERSE(N);
  int ret = 0;
  REP(i, N.length()) {
    ret *= 10;
    ret += (N[i] - '0');
  }
  return ret;
}

int make_S(string N) {
  SORT(N);
  int ret = 0;
  REP(i, N.length()) {
    ret *= 10;
    ret += (N[i] - '0');
  }
  return ret;
}

int main() {
  string N;
  while (cin >> N && N != "0000") {
    int cnt = 0;
    if (isNG(N)) {
      cout << "NA" << endl;
      continue;
    }
    while (N != END) {
      int L = make_L(N);
      int S = make_S(N);
      // cout <<L <<", " <<S <<endl;
      stringstream ss;
      ss << (L - S);
      N = ss.str();
      ++cnt;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

vector<int> solve(vector<int> a, vector<int> b) {
  string x, y;
  rep(i, a.size()) { x += '0' + a[i]; }
  rep(i, b.size()) { y += '0' + b[i]; }
  int r = stoi(x) - stoi(y);
  vector<int> ret;
  stringstream ss;
  ss << r;
  string z = ss.str();
  string t = "";
  rep(i, 4 - z.size()) t += "0";
  z = t + z;
  rep(i, z.size()) ret.pb(z[i] - '0');
  return ret;
}

int main() {
  string s;
  while (cin >> s) {
    if (s == "0000")
      break;
    int x = 0;
    vector<int> c;
    rep(i, s.size()) c.pb(s[i] - '0');
    while (1) {
      if (c[0] == 6 && c[1] == 1 && c[2] == 7 && c[3] == 4) {
        cout << x << endl;
        break;
      }
      if (c[0] == c[1] && c[1] == c[2] && c[2] == c[3]) {
        cout << "NA" << endl;
        break;
      }
      vector<int> l = c, s = c;
      sort(all(s));
      sort(all(l), greater<int>());
      c = solve(l, s);
      x++;
    }
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

vector<int> solve(vector<int> a, vector<int> b) {
  string x, y;
  rep(i, a.size()) { x += '0' + a[i]; }
  rep(i, b.size()) { y += '0' + b[i]; }
  int r = stoi(x) - stoi(y);
  vector<int> ret;
  stringstream ss;
  ss << r;
  string z = ss.str();
  rep(i, z.size()) ret.pb(z[i] - '0');
  return ret;
}

int main() {
  string s;
  while (cin >> s) {
    if (s == "0000")
      break;
    int x = 0;
    vector<int> c;
    rep(i, s.size()) c.pb(s[i] - '0');
    while (1) {
      if (c[0] == 6 && c[1] == 1 && c[2] == 7 && c[3] == 4) {
        cout << x << endl;
        break;
      }
      if (c[0] == c[1] && c[1] == c[2] && c[2] == c[3]) {
        cout << "NA" << endl;
        break;
      }
      vector<int> l = c, s = c;
      sort(all(s));
      sort(all(l), greater<int>());
      c = solve(l, s);
      x++;
    }
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int func(string n) {
  if (n[0] == n[1] && n[1] == n[2] && n[2] == n[3])
    return -1;

  string L, S;

  for (int cnt = 0;; cnt++) {
    if (n == "6174")
      return cnt;
    L = S = n;
    sort(L.begin(), L.end(), greater<char>());
    sort(S.begin(), S.end());
    int x = atoi(L.c_str()) - atoi(S.c_str());
    for (int i = 0, p = 1000; i < 4; i++, p /= 10) {
      n[i] = '0' + x / p;
      x %= p;
    }
  }
}

int main() {
  while (1) {
    string N;
    cin >> N;
    if (N == "0000")
      break;
    int ans = func(N);
    if (ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int N;
  while (1) {
    cin >> N;
    if (N == 0000)
      break;
    if (N % 1111 == 0) {
      cout << "NA" << endl;
    }
    int cnt = 0;

    while (N != 6174) {
      int n[4];
      n[0] = N / 1000;
      n[1] = N / 100 % 10;
      n[2] = N / 10 % 10;
      n[3] = N % 10;
      sort(n, n + 4);
      int L = n[3] * 1000 + n[2] * 100 + n[1] * 10 + n[0];
      int S = n[0] * 1000 + n[1] * 100 + n[2] * 10 + n[3];
      N = L - S;
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int s(int n, bool f) {
  vector<int> a;
  for (int i = 0; i < 4; ++i) {
    a.push_back(n % 10);
    n /= 10;
  }

  sort(a.begin(), a.end());
  if (f)
    reverse(a.begin(), a.end());

  int res = 0;
  for (int i = 0; i < 4; ++i) {
    res *= 10;
    res += a[i];
  }

  return res;
}

int main() {
  int n;

  while (cin >> n, n) {
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }

    // cout << s(n, true) - s(n, false) << endl;

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      n = s(n, true) - s(n, false);
    }

    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

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

int s(int n, bool f) {
  vector<int> a;
  while (n) {
    a.push_back(n % 10);
    n /= 10;
  }

  sort(a.begin(), a.end());
  if (f)
    reverse(a.begin(), a.end());

  int res = 0;
  for (int i = 0; i < a.size(); ++i) {
    res *= 10;
    res += a[i];
  }

  return res;
}

int main() {
  int n;

  while (cin >> n, n) {
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }

    // cout << s(n, true) - s(n, false) << endl;

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      n = s(n, true) - s(n, false);
    }

    cout << cnt << endl;
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>

using namespace std;

void solve() {
  string str;
  while (cin >> str) {
    if (str == "0000") {
      break;
    } else if (atoi(str.c_str()) % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int count = 0;
    while (str != "6174") {
      sort(str.begin(), str.end());
      string small = str;
      string large;
      for (int i = str.size() - 1; i >= 0; --i) {
        large += str[i];
      }
      int num = atoi(large.c_str()) - atoi(small.c_str());
      stringstream ss;
      ss << num;
      str = ss.str();
      if (str.size() < 4) {
        string buffer = str;
        str.clear();
        for (int i = 0; i < 4 - buffer.size(); ++i) {
          str += "0";
        }
        str += buffer;
      }
      ++count;
    }
    cout << count << endl;
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>

using namespace std;

void solve() {
  string str;
  while (cin >> str) {
    if (str == "0000") {
      break;
    } else if (atoi(str.c_str()) % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int count = 0;
    while (str != "6174") {
      sort(str.begin(), str.end());
      string small = str;
      string large;
      for (int i = str.size() - 1; i >= 0; --i) {
        large += str[i];
      }
      int num = atoi(large.c_str()) - atoi(small.c_str());
      stringstream ss;
      ss << num;
      str = ss.str();
      ++count;
    }
    cout << count << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p00254 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  char c[5];
  while (scanf("%s", c),
         !(c[0] == '0' && c[1] == '0' && c[2] == '0' && c[3] == '0')) {
    if (c[0] == c[1] && c[0] == c[2] && c[0] == c[3]) {
      printf("NA\\n");
      continue;
    }
    int res = 0;
    while (!(c[0] == '6' && c[1] == '1' && c[2] == '7' && c[3] == '4')) {
      char l[5], m[5];
      for (int i = 0; i < 5; i++)
        l[i] = m[i] = c[i];
      sort(l, l + 4, greater<char>());
      sort(m, m + 4);
      int a = (l[0] - 48) * 1000 + (l[1] - 48) * 100 + (l[2] - 48) * 10 +
              (l[3] - 48);
      int b = (m[0] - 48) * 1000 + (m[1] - 48) * 100 + (m[2] - 48) * 10 +
              (m[3] - 48);
      int x = a - b;
      for (int i = 0, p = 1000; i < 4; i++) {
        c[i] = (char)(48 + x / p);
        x %= p;
        p /= 10;
      }
      res++;
    }
    printf("%d\\n", res);
  }
}

Error: -8

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  string c;
  while (cin >> c, c != "0000") {
    if (c[0] == c[1] && c[0] == c[2] && c[0] == c[3]) {
      printf("NA\\n");
      continue;
    }
    int res = 0;
    while (c != "6174") {
      cout << c << endl;
      string l, m;
      l = m = c;
      sort(l.begin(), l.end(), greater<char>());
      sort(m.begin(), m.end());
      int a = (l[0] - 48) * 1000 + (l[1] - 48) * 100 + (l[0] - 48) * 10 +
              (l[1] - 48);
      int b = (m[0] - 48) * 1000 + (m[1] - 48) * 100 + (m[0] - 48) * 10 +
              (m[1] - 48);
      int x = a - b;
      for (int i = 0, p = 1000; i < 4; i++) {
        c[i] = (char)(48 + x / p);
        p %= 10;
      }
      res++;
    }
    printf("%d\\n", res);
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string toString(int n) {
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s) {
  istringstream iss(s);
  int ret;
  iss >> ret;
  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;
    bool f = false;
    while (s != "6174") {
      if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
        cout << "NA" << endl;
        f = true;
        break;
      }
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a - b);
      while (s.size() != 4) {
        s += "0";
      }
    }
    if (!f)
      cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string toString(int n) {
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s) {
  istringstream iss(s);
  int ret;
  iss >> ret;
  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") {
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a - b);
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string S;
int a[4], b, cnt;
int main() {
  while (true) {
    cin >> S;
    if (S == "0000") {
      break;
    }
    if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) {
      cout << "NA" << endl;
    } else {
      b = stoi(S);
      cnt = 0;
      while (b != 6174) {
        cnt++;
        for (int i = 0; i < 4; i++) {
          a[i] = (b / (int)pow(10, i)) % 10;
        }
        sort(a, a + 4);
        b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 +
            (a[0] - a[3]);
      }
      cout << cnt << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string S;
int a[4], b, cnt;
int main() {
  while (true) {
    cin >> S;
    if (S == "0000") {
      break;
    }
    if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) {
      cout << "NA" << endl;
    } else {
      b = stoi(S);
      cnt = 0;
      while (b != 6174) {
        cnt++;
        for (int i = 0; i < 4; i++) {
          a[i] = (b / (int)pow(10, i)) % 10;
        }
        sort(a, a + 4);
        b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 +
            (a[0] + a[3]);
      }
      cout << cnt << endl;
    }
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main(void) {
  int n, h[10], s[10], k[10], i, j, temp, flg, cnt;
  while (1) {
    flg = 0;
    cnt = 0;
    scanf("%d", &n);
    if (n == 0000)
      break;
    while (1) {
      if (n == 6174)
        break;
      if (n < 1000) {
        h[0] = 0;
      } else {
        h[0] = n / 1000;
      }
      if (n < 100) {
        h[1] = 0;
      } else {
        h[1] = (n % 1000) / 100;
      }
      if (n < 10) {
        h[2] = 0;
      } else {
        h[2] = ((n % 1000) % 100) / 10;
      }
      h[3] = (((n % 1000) % 100) % 10) / 1;
      if (h[0] == h[1] && h[0] == h[2] && h[0] == h[3]) {
        flg = 1;
        printf("NA\\n");
        break;
      }
      cnt++;
      //			printf("%d\\n",cnt);
      for (i = 0; i < 4; i++) {
        s[i] = h[i];
        k[i] = h[i];
      }
      for (i = 0; i < 3; i++) {
        for (j = i + 1; j < 4; j++) {
          if (s[i] > s[j]) {
            temp = s[i];
            s[i] = s[j];
            s[j] = temp;
          }
        }
      }
      for (i = 0; i < 3; i++) {
        for (j = i + 1; j < 4; j++) {
          if (k[i] < k[j]) {
            temp = k[i];
            k[i] = k[j];
            k[j] = temp;
          }
        }
      }
      n = (k[0] * 1000 + k[1] * 100 + k[2] * 10 + k[3]) -
          (s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3]);
    }
    if (flg != 1) {
      printf("%d\\n", cnt);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main(void) {
  int n, h[4], s[4], k[4], i, j, temp, flg, cnt;
  while (1) {
    cnt = 0;
    scanf("%d", &n);
    if (n == 0000)
      break;
    if (n < 1000) {
      h[0] = 0;
    } else {
      h[0] = n / 1000;
    }
    if (n < 100) {
      h[1] = 0;
    } else {
      h[1] = (n % 1000) / 100;
    }
    if (n < 10) {
      h[2] = 0;
    } else {
      h[2] = ((n % 1000) % 100) / 10;
    }
    h[3] = (((n % 1000) % 100) % 10) / 1;
    while (1) {
      if (h[0] == h[1] && h[0] == h[2] && h[0] == h[3]) {
        printf("NA\\n");
        break;
      }
      if (n == 6174)
        break;
      if (n < 1000) {
        h[0] = 0;
      } else {
        h[0] = n / 1000;
      }
      if (n < 100) {
        h[1] = 0;
      } else {
        h[1] = (n % 1000) / 100;
      }
      if (n < 10) {
        h[2] = 0;
      } else {
        h[2] = ((n % 1000) % 100) / 10;
      }
      h[3] = (((n % 1000) % 100) % 10) / 1;
      cnt++;
      for (i = 0; i < 4; i++) {
        s[i] = h[i];
        k[i] = h[i];
      }
      for (i = 0; i < 3; i++) {
        for (j = i + 1; j < 4; j++) {
          if (s[i] > s[j]) {
            temp = s[i];
            s[i] = s[j];
            s[j] = temp;
          }
        }
        for (j = i + 1; j < 4; j++) {
          if (k[i] > k[j]) {
            temp = k[i];
            k[i] = k[j];
            k[j] = temp;
          }
        }
      }
    }
    if (cnt > 0) {
      printf("%d\\n", cnt);
    }
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  string S;

  while (true) {
    cin >> S;

    if (S == "0000") {
      break;
    }

    if (S[0] == S[1] && S[1] == S[2] && S[2] == S[3]) {
      cout << "NA" << endl;
    } else {
      int count_ = 0;

      while (S != "6174") {
        string S1 = S, S2 = S;

        sort(S1.begin(), S1.end(), greater<char>());
        sort(S2.begin(), S2.end());

        S = to_string(stoi(S1) - stoi(S2));

        while (S.size() < 4) {
          S = '0' + S;
        }

        count_++;
      }

      cout << count_ << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  string S;

  while (true) {
    cin >> S;

    if (S == "0000") {
      break;
    }

    if (S[0] == S[1] == S[2] == S[3]) {
      cout << "NA" << endl;
    } else {
      int count_ = 0;

      while (S != "6174") {
        string S1 = S, S2 = S;

        sort(S1.begin(), S1.end(), greater<char>());
        sort(S2.begin(), S2.end());

        S = to_string(stoi(S1) - stoi(S2));

        while (S.size() < 4) {
          S = '0' + S;
        }

        count_++;
      }

      cout << count_ << endl;
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

bool check(string &s) { return (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]); }

int main() {
  string N;
  while (cin >> N, N != "0000") {
    int cnt = 0;
    bool fail = 0;
    while (N != "6174") {
      if (check(N)) {
        fail = 1;
        break;
      }
      string L = N, S = N;
      sort(L.begin(), L.end(), greater<char>());
      sort(S.begin(), S.end());

      int diff = stoi(L) - stoi(S);
      N = to_string(diff);
      reverse(N.begin(), N.end());
      while (N.size() != 4) {
        N += "0";
      }
      reverse(N.begin(), N.end());
      cnt++;
    }
    if (fail) {
      cout << "NA" << endl;
    } else {
      cout << cnt << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

bool check(string &s) { return (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]); }

int main() {
  string N;
  while (cin >> N, N != "0000") {
    int cnt = 0;
    bool fail = 0;
    while (N != "6174") {
      if (check(N)) {
        fail = 1;
        break;
      }
      string L = N, S = N;
      sort(L.begin(), L.end(), greater<char>());
      sort(S.begin(), S.end());
      auto diff = stoi(L) - stoi(S);
      N = to_string(diff);
      cnt++;
    }
    if (fail) {
      cout << "NA" << endl;
    } else {
      cout << cnt << endl;
    }
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;

int main() {
  std::string s;
  while (std::cin >> s, s != "0000") {
    if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
      std::cout << "NA" << std::endl;
    } else {
      int t = 0;
      while (s != "6174") {
        int L, S;
        std::sort(s.begin(), s.end(), std::greater<char>());
        L = std::stoi(s);
        std::sort(s.begin(), s.end());
        S = std::stoi(s);

        s = std::to_string(L - S);
        while (s.length() < 4) {
          s += "0";
        }
        t++;
      }

      std::cout << t << std::endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;

int main() {
  std::string s;
  while (std::cin >> s, s != "0000") {
    if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
      std::cout << "NA" << std::endl;
    } else {
      int t = 0;
      while (s != "6174") {
        int L, S;
        std::sort(s.begin(), s.end(), std::greater<char>());
        L = std::stoi(s);
        std::sort(s.begin(), s.end());
        S = std::stoi(s);

        s = std::to_string(L - S);
        t++;
      }

      std::cout << t << std::endl;
    }
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()

int main() {
  int a;
  while (cin >> a && a) {
    if (a % 1111 == 0) {
      printf("NA\\n");
      continue;
    }
    int ans = 0;
    while (a != 6174) {
      int d[4];
      rep(i, 4) {
        d[i] = a % 10;
        a /= 10;
      }
      sort(d, d + 4);
      int b = 0;
      rep(i, 4) {
        b *= 10;
        b += d[i];
      }
      int c = 0;
      for (int i = 3; i >= 0; i--) {
        c *= 10;
        c += d[i];
      }
      a = c - b;
      ans++;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    int c;
    while (n != 6174) {
      stringstream ss;
      ss << n;
      string buf = ss.str();
      string strs = buf;
      string strl = buf;
      sort(strs.begin(), strs.end());
      sort(strl.begin(), strl.end(), greater<int>());
      int s = atoi(strs.c_str());
      int l = atoi(strl.c_str());
      n = l - s;
      c++;
    }
    cout << c << endl;
  }
  return 0;
}