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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

int main() {
  int n, t, f;
  int h[101];
  for (;;) {
    scanf("%d", &n);
    if (!n)
      break;
    n++;
    rep(i, n) scanf("%d", i + h);
    rep(i, n) {
      f = 1;
      t = h[1 + (i < 2)] - h[i == 0];
      rep(j, n - 2) {
        if (t != h[j + 1 + (j + 1 >= i)] - h[j + (j >= i)]) {
          f = 0;
          break;
        }
      }
      if (f) {
        printf("%d\\n", h[i]);
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

int main() {
  int n, t, f;
  int h[101];
  for (;;) {
    scanf("%d", &n);
    n++;
    rep(i, n) scanf("%d", i + h);
    rep(i, n) {
      f = 1;
      t = h[1 + (i < 2)] - h[i == 0];
      rep(j, n - 2) {
        if (t != h[j + 1 + (j + 1 >= i)] - h[j + (j >= i)]) {
          f = 0;
          break;
        }
      }
      if (f) {
        printf("%d\\n", h[i]);
        break;
      }
    }
  }
  return 0;
}
Problem p00253 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N;
  while (cin >> N && N) {
    vector<int> a(N + 1);
    vector<int> ans;
    REP(i, N + 1) cin >> a[i];
    REP(i, N + 1) {
      vector<int> b;
      REP(j, N + 1) if (i != j) b.push_back(a[j]);
      set<int> s;
      REP(j, b.size() - 1) s.insert(b[j + 1] - b[j]);
      if (s.size() == 1) {
        ans.push_back(a[i]);
      }
    }
    // assert(ans.size() == 1);
    cout << ans[0] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N;
  while (cin >> N && N) {
    vector<int> a(N + 1);
    vector<int> ans;
    REP(i, N + 1) cin >> a[i];
    REP(i, N + 1) {
      vector<int> b;
      REP(j, N + 1) if (i != j) b.push_back(a[j]);
      set<int> s;
      REP(j, b.size() - 1) s.insert(b[j + 1] - b[j]);
      if (s.size() == 1) {
        ans.push_back(a[i]);
      }
    }
    assert(ans.size() == 1);
    cout << ans[0] << endl;
  }
  return 0;
}
Problem p00253 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int inf = 1000000001;
int n;
int t[101];
int u[101];

int check(int x) {
  int i = 0, a = inf;
  for (i = 1; i <= n; i++) {
    if (i == x + 1)
      continue;
    if (a == inf)
      a = u[i] - u[i - 1];
    if (a != u[i] - u[i - 1])
      return 0;
  }
  return 1;
}

void solve() {
  int i;
  for (i = 0; i < n; i++) {
    u[i] = u[i + 1];
    if (check(i)) {
      printf("%d\\n", t[i]);
      return;
    }
    u[i] = t[i];
  }
  printf("%d\\n", t[n]);
}

int main() {
  int i;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i <= n; i++) {
      scanf("%d", &t[i]);
      u[i] = t[i];
    }
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
int n;
int t[102];
int u[102];

int check(int x) {
  int i = 0, a = -1;
  for (i = 1; i <= n; i++) {
    if (i == x + 1)
      continue;
    if (a == -1)
      a = u[i] - u[i - 1];
    if (a != u[i] - u[i])
      return 0;
  }
  return 1;
}

int main() {
  int i;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i <= n; i++) {
      scanf("%d", t[i]);
      u[i] = t[i];
    }
    for (i = 0; i <= n; i++) {
      u[i] = u[i + 1];
      if (check(i)) {
        printf("%d\\n", t[i]);
        break;
      }
      u[i] = t[i];
    }
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

int func2(int *temp) {
  int n = 0;

  n += (temp[0] * 1000);
  n += (temp[1] * 100);
  n += (temp[2] * 10);
  n += temp[3];

  return n;
}

int main() {
  while (true) {
    int N = 0, Nm[4] = {0};
    unsigned int count = 0;
    int judge = 0;
    int Lm[4] = {0}, Sm[4] = {0};
    int S = 0, L = 0;

    cin >> N;

    Nm[0] = N / 1000;
    N %= 1000;
    Nm[1] = N / 100;
    N %= 100;
    Nm[2] = N / 10;
    N %= 10;
    Nm[3] = N;

    if ((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))
      break;

    for (int i = 0; i < 4; i++) {
      if (Nm[i] == Nm[0])
        judge++;
    }

    if (judge != 4) {
      while (true) {
        if ((Nm[0] == 6 && Nm[1] == 1) && (Nm[2] == 7 && Nm[3] == 4))
          break;

        // Sを作成
        sort(Nm, Nm + 4);
        for (int i = 0; i < 4; i++) {
          Sm[i] = Nm[i];
          // cout << Sm[i] << endl;
        }
        S = func2(Sm);

        // Lを作成
        sort(Nm, Nm + 4, greater<int>());
        for (int i = 0; i < 4; i++) {
          Lm[i] = Nm[i];
        }
        L = func2(Lm);

        N = L - S;
        count++;

        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
      }
      cout << count << endl;
    }

    if (judge == 4) {
      cout << "NA" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

int func2(int *temp) {
  int n = 0;

  n += (temp[0] * 1000);
  n += (temp[1] * 100);
  n += (temp[2] * 10);
  n += temp[3];

  return n;
}

int main() {
  while (true) {
    int N = 0, Nm[4] = {0};
    unsigned int count = 0;
    int judge = 0;
    int Lm[4] = {0}, Sm[4] = {0};
    int S = 0, L = 0;

    cin >> N;

    Nm[0] = N / 1000;
    N %= 1000;
    Nm[1] = N / 100;
    N %= 100;
    Nm[2] = N / 10;
    N %= 10;
    Nm[3] = N;

    if ((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))
      break;

    for (int i = 1; i < 4; i++) {
      if (Nm[i] == Nm[0])
        judge++;
    }

    if (judge != 4) {
      while (true) {
        if ((Nm[0] == 6 && Nm[1] == 1) && (Nm[2] == 7 && Nm[3] == 4))
          break;

        // Sを作成
        sort(Nm, Nm + 4);
        for (int i = 0; i < 4; i++) {
          Sm[i] = Nm[i];
          // cout << Sm[i] << endl;
        }
        S = func2(Sm);

        // Lを作成
        sort(Nm, Nm + 4, greater<int>());
        for (int i = 0; i < 4; i++) {
          Lm[i] = Nm[i];
        }
        L = func2(Lm);

        N = L - S;
        count++;

        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
      }
      cout << count << endl;
    }

    if (judge == 4) {
      cout << "NA" << endl;
    }
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Runtime Error

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

int to_i(string &s) {
  int a = 0;
  for (int i = 0; i < s.size(); i++)
    a = a * 10 + s[i] - '0';
  return a;
}
string to_s(int n) {
  string s;
  while (n) {
    s += n % 10 + '0';
    n /= 10;
  }
  while (s.size() < 4)
    s += '0';
  reverse(s.begin(), s.end());
  return s;
}

int cache[10000];

int solve(int n) {
  if (n == 6174)
    return 0;
  if (cache[n])
    return cache[n];
  string s = to_s(n);
  int a, b;
  sort(s.begin(), s.end());
  a = to_i(s);
  sort(s.begin(), s.end(), greater<char>());
  b = to_i(s);
  return cache[n] = 1 + solve(b - a);
}

int main() {
  string s;
  while (cin >> s && s != "0000") {
    if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
      cout << "NA\\n";
      continue;
    }
    cout << solve(to_i(s)) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int to_i(string &s) {
  int a = 0;
  for (int i = 0; i < s.size(); i++)
    a = a * 10 + s[i] - '0';
  return a;
}
string to_s(int n) {
  string s;
  while (n) {
    s += n % 10 + '0';
    n /= 10;
  }
  reverse(s.begin(), s.end());
  return s;
}

int cache[10000];

int solve(int n) {
  if (n == 6174)
    return 0;
  if (cache[n])
    return n;
  string s = to_s(n);
  int a, b;
  sort(s.begin(), s.end());
  a = to_i(s);
  sort(s.begin(), s.end(), greater<char>());
  b = to_i(s);
  return cache[n] = 1 + solve(b - a);
}

int main() {
  string s;
  while (cin >> s && s != "0000") {
    if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
      cout << "NA\\n";
      continue;
    }
    cout << solve(to_i(s)) << endl;
  }
  return 0;
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  std::string N;
  while (std::cin >> N, N != "0000") {
    int cnt = 0;

    bool f = true;
    for (int i = 0; i != 3; ++i)
      f &= N[i] == N[i + 1];

    if (f)
      std::cout << "NA" << std::endl;
    else {
      while (N != "6174") {
        std::string L = N;
        std::sort(L.rbegin(), L.rend());

        std::string S = L;
        std::reverse(S.begin(), S.end());

        std::ostringstream oss;
        oss << std::setw(4) << std::setfill('0')
            << atoi(L.c_str()) - atoi(S.c_str());
        N = oss.str();

        ++cnt;
      }

      std::cout << cnt << std::endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  std::string N;
  while (std::cin >> N, N != "0000") {
    int cnt = 0;

    while (N != "6174") {
      std::string L = N, S = N;

      std::sort(L.rbegin(), L.rend());
      std::sort(S.begin(), S.end());

      for (int i = 0; i != N.size(); ++i) {
        N[i] = L[i] - S[i] + '0';
        if (i > 0 && N[i] < '0')
          --N[i - 1], N[i] += 10;
      }

      ++cnt;

      if (N == "0000") {
        std::cout << "NA" << std::endl;
        break;
      }
    }

    if (N != "0000")
      std::cout << cnt << std::endl;
  }

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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <sstream>
using namespace std;

string to_str(int n) {
  string res;
  res.push_back('0' + n % 10000 / 1000);
  res.push_back('0' + n % 1000 / 100);
  res.push_back('0' + n % 100 / 10);
  res.push_back('0' + n % 10 / 1);
  return res;
}

bool same_all(string s) { return count(s.begin(), s.end(), s[0]) == s.size(); }
int to_int(string s) { return atoi(s.data()); }

int main() {
  for (string line; cin >> line && (line != "0000");) {
    int times = 0;

    if (same_all(line)) {
      cout << "NA" << endl;
      continue;
    }

    for (; line != "6174"; times++) {
      sort(line.begin(), line.end());
      int small = to_int(line);

      reverse(line.begin(), line.end());
      int big = to_int(line);

      int next = big - small;

      line = to_str(next);
    }

    cout << times << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <sstream>
using namespace std;

string to_str(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
bool same_all(string s) { return count(s.begin(), s.end(), s[0]) == s.size(); }
int to_int(string s) { return atoi(s.data()); }

int main() {
  for (string line; cin >> line && (line != "0000");) {
    int times = 0;

    if (same_all(line)) {
      cout << "NA" << endl;
      continue;
    }

    for (; line != "6174"; times++) {
      sort(line.begin(), line.end());
      int small = to_int(line);

      reverse(line.begin(), line.end());
      int big = to_int(line);

      int next = big - small;

      assert(next >= 0);

      line = to_str(next);
    }

    cout << times << endl;
  }
}
Problem p00254 (C++) — buggy submission, verdict: Runtime Error

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

bool allsame(string s) {
  char c = s[0];
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != c)
      return 0;
  }
  return 1;
}

string toSortString(int n) {
  string res = "0000";
  int tmp[4] = {0, 0, 0, 0};
  for (int i = 0; n != 0; i++) {
    tmp[i] = n % 10;
    n /= 10;
  }
  for (int i = 0; i < 4; i++) {
    res[i] = tmp[i] + '0';
  }
  sort(res.begin(), res.end());
  return res;
}

int make_l(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) {
    res += (s[i] - '0') * pow(double(10), i);
  }
  return res;
}

int make_s(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) {
    res += (s[s.size() - 1 - i] - '0') * pow(double(10), i);
  }
  return res;
}

int main() {
  int n;
  int ans = 0;
  while (cin >> n, n != 0) {
    ans = 0;
    while (n != 6174) {
      string tmp_n = toSortString(n);
      if (allsame(tmp_n)) {
        cout << "NA" << endl;
        break;
      }
      ans++;
      int l, s;
      l = make_l(tmp_n);
      s = make_s(tmp_n);
      n = l - s;
      assert(ans < 100);
    }
    if (n == 6174) {
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

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

bool allsame(int n) {
  int tmp = n % 10;
  n /= 10;
  while (n != 0) {
    if (tmp != n % 10) {
      return 0;
    }
    tmp = n % 10;
    n /= 10;
  }
  return 1;
}

string toSortString(int n) {
  string res = "0000";
  int tmp[4] = {0, 0, 0, 0};
  for (int i = 0; n != 0; i++) {
    tmp[i] = n % 10;
    n /= 10;
  }
  for (int i = 0; i < 4; i++) {
    res[i] = tmp[i] + '0';
  }
  sort(res.begin(), res.end());
  return res;
}

int make_l(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) {
    res += (s[i] - '0') * pow(double(10), i);
  }
  return res;
}

int make_s(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) {
    res += (s[s.size() - 1 - i] - '0') * pow(double(10), i);
  }
  return res;
}

int main() {
  int n;
  int ans = 0;
  while (cin >> n, n != 0) {
    while (n != 6174) {
      if (allsame(n)) {
        cout << "NA" << endl;
        break;
      }
      ans++;
      int l, s;
      string tmp_n = toSortString(n);
      l = make_l(tmp_n);
      s = make_s(tmp_n);
      n = l - s;
      assert(ans <= 7);
    }
    if (n == 6174) {
      cout << ans << endl;
    }
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

int main() {
  int x, a, b;
  string s;
  while (scanf("%d", &x), x) {
    if (x % 1111 == 0) {
      puts("NA");
    } else {
      int ans;
      for (ans = 0; x != 6174; ++ans) {
        convert(x, s);
        s.resize(4, '0');
        sort(s.begin(), s.end());
        convert(s, a);
        reverse(s.begin(), s.end());
        convert(s, b);
        x = b - a;
      }
      printf("%d\\n", ans);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

int main() {
  int x, a, b;
  string s;
  while (scanf("%d", &x), x) {
    if (x % 1111 == 0) {
      puts("NA");
    } else {
      int ans;
      for (ans = 0; x != 6174; ++ans) {
        convert(x, s);
        sort(s.begin(), s.end());
        convert(s, a);
        reverse(s.begin(), s.end());
        convert(s, b);
        x = b - a;
      }
      printf("%d\\n", ans);
    }
  }
}
Problem p00254 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#define rep(i, a) for (int i = 0; i != (a); ++i)

std::string intToStr(int n) {
  char str[5];
  sprintf(str, "%04d", n);

  return str;
}

int main() {
  std::string L, S;
  while (std::cin >> L, L != "0000") {
    bool fl = true;

    rep(i, 3) fl &= L[i + 1] == L[i];
    if (fl) {
      std::cout << "NA" << std::endl;
      continue;
    }

    int ans = 0;
    while (L != "6174") {
      S = L;
      std::sort(L.rbegin(), L.rend());
      std::sort(S.begin(), S.end());

      ++ans;

      L = intToStr(atoi(L.c_str()) - atoi(S.c_str()));
    }

    std::cout << ans << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#define rep(i, a) for (int i = 0; i != (a); ++i)

std::string intToStr(int n) {
  std::ostringstream oss;
  oss << std::setprecision(4) << std::setfill('0') << n;

  return oss.str();
}

int main() {
  std::string L, S;
  while (std::cin >> L, L != "0000") {
    bool fl = true;

    rep(i, 3) fl &= L[i + 1] == L[i];
    if (fl) {
      std::cout << "NA" << std::endl;
      continue;
    }

    int ans = 0;
    while (L != "6174") {
      S = L;
      std::sort(L.rbegin(), L.rend());
      std::sort(S.begin(), S.end());

      ++ans;

      L = intToStr(atoi(L.c_str()) - atoi(S.c_str()));
    }

    std::cout << ans << std::endl;
  }

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

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

int main() {
  string str;
  while (cin >> str) {
    bool f = false;
    for (auto i : str)
      if (i != '0')
        f = true;

    if (!f)
      break;

    f = false;
    for (auto i : str)
      if (i != str[0])
        f = true;

    if (!f) {
      cout << "NA" << endl;
      continue;
    }

    int count = 0;
    int now;
    stringstream org;
    org << str;
    org >> now;
    while (1) {
      if (now == 6174) {
        cout << count << endl;
        break;
      }
      count++;
      sort(str.begin(), str.end());
      auto rstr = str;
      reverse(rstr.begin(), rstr.end());

      stringstream ss;
      ss << rstr << " " << str;
      int A, B;
      ss >> A >> B;
      now = A - B;
      stringstream next;
      next << now;
      next >> str;
      while (str.size() < 4)
        str = "0" + str;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string str;
  while (cin >> str) {
    bool f = false;
    for (auto i : str)
      if (i != '0')
        f = true;

    if (!f)
      break;

    f = false;
    for (auto i : str)
      if (i != str[0])
        f = true;

    if (!f) {
      cout << "NA" << endl;
      continue;
    }

    int count = 0;
    int now;
    stringstream org;
    org << str;
    org >> now;
    while (1) {
      if (now == 6174) {
        cout << count << endl;
        break;
      }
      count++;
      sort(str.begin(), str.end());
      auto rstr = str;
      reverse(rstr.begin(), rstr.end());

      stringstream ss;
      ss << rstr << " " << str;
      int A, B;
      ss >> A >> B;
      now = A - B;
      stringstream next;
      next << now;
      next >> str;
    }
  }

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

#include <bits/stdc++.h>
#define FOR(v, a, b) for (int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str)                                                      \\
  for (int i = 0; i < (vec).size(); ++i)                                       \\
  cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec)                                                           \\
  for (int i = 0; i < (vec).size(); ++i)                                       \\
  cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;

int main() {
  int n, cnt;

  int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
  vector<int> repdig(rep, rep + (sizeof(rep) / sizeof(int)));

  while (cin >> n, n) {

    if (find(repdig.begin(), repdig.end(), n) != repdig.end()) {
      cout << "NA" << endl;
      continue;
    }

    cnt = 0;

    while (n != 6174) {
      stringstream ss;
      ss << setfill('0') << setw(4) << n;
      string str = ss.str();

      sort(str.begin(), str.end());

      int s = atoi(str.c_str());

      reverse(str.begin(), str.end());

      int l = atoi(str.c_str());

      n = l - s;
      ++cnt;
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(v, a, b) for (int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str)                                                      \\
  for (int i = 0; i < (vec).size(); ++i)                                       \\
  cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec)                                                           \\
  for (int i = 0; i < (vec).size(); ++i)                                       \\
  cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;

int main() {
  int n, cnt;

  int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
  vector<int> repdig(rep, rep + (sizeof(rep) / sizeof(int)));

  while (cin >> n, n) {

    if (find(repdig.begin(), repdig.end(), n) != repdig.end()) {
      cout << "NA" << endl;
      continue;
    }

    cnt = 0;

    while (n != 6174) {
      stringstream ss;
      ss << n;
      string str = ss.str();

      sort(str.begin(), str.end());

      int s = atoi(str.c_str());

      reverse(str.begin(), str.end());

      int l = atoi(str.c_str());

      n = l - s;
      ++cnt;
    }
    cout << cnt << endl;
  }

  return 0;
}