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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int main() {
  int n;
  int cnt;
  int num[4];
  while (scanf("%d", &n) && n) {
    cnt = 0;
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    while (n != 6174) {
      cnt++;
      int tmp = n;
      for (int i = 0; i < 4; i++) {
        num[i] = tmp % 10;
        tmp /= 10;
      }
      int x = 0;
      sort(num, num + 4, greater<int>());
      for (int i = 0; i < 4; i++) {
        x = x * 10 + num[i];
      }
      int L, S;
      L = x;
      sort(num, num + 4);
      x = 0;
      for (int i = 0; i < 4; i++) {
        x = x * 10 + num[i];
      }
      S = x;
      n = L - S;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int main() {
  string str;
  int n;
  while (cin >> str) {
    if (str == "0000")
      return 0;
    stringstream ss(str);
    n = atoi(ss.str().c_str());
    // cout <<n<<endl;
    vector<int> L;
    vector<int> S;
    int cnt = 0;
    bool ok = true;
    while (n != 6174) {
      cnt++;
      L.clear();
      S.clear();
      // cout <<n<<endl;
      while (n) {
        int x;
        x = (n % 10);
        n /= 10;
        L.push_back(x);
      }
      S = L;
      sort(S.begin(), S.end());
      sort(L.begin(), L.end(), greater<int>());
      /*for(int i =0;i <L.size();i++){
              cout <<S[i]<<endl;
      }
      cout <<endl;
      for(int i =0;i <S.size();i++){
              cout <<L[i]<<endl;
      }
      cout <<endl;*/
      int c = 4;
      int l, s;
      l = s = 0;
      while (c--) {
        l += (L[3 - c] * pow(10.0, c));
        s += (S[3 - c] * pow(10.0, c));
      }
      n = (l - s);
      // cout <<l<<" "<<s<<endl;
      /*if(cnt==3){
              return 0;
      }*/
      if (n == 0) {
        cout << "NA" << endl;
        ok = false;
        break;
      }
    }
    if (ok) {
      cout << cnt << endl;
    }
  }
  return 0;
}