alexjercan/bugnet huggingface dataset:
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> a;
int L;

string intString(int n) {
  stringstream ss;
  int keta = 1, tmp;
  tmp = n;
  while (tmp != 0) {
    tmp /= 10;
    keta++;
  }
  for (int i = 0; i <= (L - keta); i++)
    ss << 0;

  ss << n;

  return ss.str();
}

int func(string str) {
  sort(str.begin(), str.end());
  int low = stoi(str);
  sort(str.rbegin(), str.rend());
  int high = stoi(str);
  return high - low;
}

void solve() {
  // 桁変更
  string str = intString(a[0]);
  for (int i = 1;; i++) {
    a.push_back(func(str));
    for (int j = 0; j < i; j++) {
      if (a[i] == a[j]) {
        cout << j << ' ' << a[i] << ' ' << (i - j) << endl;
        a.clear();
        return;
      }
    }
    str = intString(a[i]);
  }
}

int main() {
  int a0;
  while (true) {
    cin >> a0 >> L;
    if (a0 + L == 0)
      break;
    a.push_back(a0);
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> a;
int L;

string intString(int n) {
  stringstream ss;
  int keta = 1, tmp;
  tmp = n;
  while (tmp != 0) {
    tmp /= 10;
    keta++;
  }
  for (int i = 0; i <= (L - keta); i++)
    ss << 0;

  ss << n;

  return ss.str();
}

int func(string str) {
  sort(str.begin(), str.end());
  int low = stoi(str);
  sort(str.rbegin(), str.rend());
  int high = stoi(str);
  return high - low;
}

void solve() {
  // 桁変更
  string str = intString(a[0]);
  for (int i = 1;; i++) {
    a[i] = func(str);
    for (int j = 0; j < i; j++) {
      if (a[i] == a[j]) {
        cout << j << ' ' << a[i] << ' ' << (i - j) << endl;
        a.clear();
        return;
      }
    }
    str = intString(a[i]);
  }
}

int main() {
  int a0;
  while (true) {
    cin >> a0 >> L;
    if (a0 + L == 0)
      break;
    a.push_back(a0);
    solve();
  }
  return 0;
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>

using namespace std;

int l;

string exchange(const string s) {
  int a, b;
  string a_s, b_s;
  stringstream s1, s2, ret;

  a_s = b_s = s;

  sort(a_s.begin(), a_s.end());
  sort(b_s.begin(), b_s.end(), greater<char>());
  s1 << a_s;
  s2 << b_s;

  s1 >> a;
  s2 >> b;
  a = b - a;
  ret << a;

  return ret.str();
}

int main() {
  string s;

  while (cin >> s >> l, s != "0" || l) {
    map<string, int> m;

    while (s.size() < l)
      s = '0' + s;
    m[s] = 0;

    for (int i = 1;; i++) {
      s = exchange(s);

      string tmp = s;
      while (tmp.size() < l)
        tmp = '0' + tmp;

      if (m.count(tmp) != 0) {
        cout << m[tmp] << " " << s << " " << i - m[tmp] << endl;
        break;
      } else
        m[tmp] = i;
      s = tmp;
    }
  }

  return 0;
}

Error: 0
2012 4
83268 6
1112 4
0 1
99 2


Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>

using namespace std;

int l;

string exchange(const string s) {
  int a, b;
  string a_s, b_s;
  stringstream s1, s2, ret;

  a_s = b_s = s;

  sort(a_s.begin(), a_s.end());
  sort(b_s.begin(), b_s.end(), greater<char>());
  s1 << a_s;
  s2 << b_s;

  s1 >> a;
  s2 >> b;
  a = b - a;
  ret << a;

  return ret.str();
}

int main() {
  string s;

  while (cin >> s >> l, s != "0" || l) {
    cerr << s << " " << l << endl;
    map<string, int> m;

    for (int i = 0; i < l - s.size(); i++)
      s = '0' + s;
    m[s] = 0;

    for (int i = 1;; i++) {
      s = exchange(s);

      string tmp = s;
      for (int j = 0; j < l - tmp.size(); j++)
        tmp = '0' + tmp;

      if (m.count(tmp) != 0) {
        cout << m[tmp] << " " << s << " " << i - m[tmp] << endl;
        break;
      } else
        m[tmp] = i;
      s = tmp;
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
vector<string> tmp;
int ans, cnt, n;
string s;
string rec(string x) {
  while (1) {
    // cout << x << endl;
    bool flag = false;
    sort(x.begin(), x.end());
    string a = x;
    if (a.empty() == 1)
      while (a.front() != '0')
        a.erase(a.begin());
    int mn = atoi(a.c_str());
    reverse(x.begin(), x.end());
    int mx = atoi(x.c_str());
    x = to_string(mx - mn);
    if (x.size() != n)
      x = '0' + x;
    // cout << mx << " " <<  mx << " " << x << endl;
    for (int i = 0; i < tmp.size(); i++) {
      // cout << tmp[i] << " ";
      if (x == tmp[i]) {
        ans = i;
        flag = true;
        break;
      }
    }
    tmp.push_back(x);
    cnt++;
    if (flag == false)
      return rec(x);
    else
      return x;
  }
  return 0;
}
int main() {
  while (1) {
    cin >> s >> n;
    if (s == "0" && n == 0)
      break;
    cnt = 0;
    ans = 0;
    while (s.size() != n)
      s = '0' + s;
    tmp.push_back(s);
    s = rec(s);
    cout << ans << " " << atoi(s.c_str()) << " " << cnt - ans << endl;
    tmp.clear();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
vector<string> tmp;
int ans, cnt, n;
string s;
string rec(string x) {
  while (1) {
    // cout << x << endl;
    bool flag = false;
    sort(x.begin(), x.end());
    string a = x;
    if (a.empty() == 1)
      while (a.front() != '0')
        a.erase(a.begin());
    int mn = atoi(a.c_str());
    reverse(x.begin(), x.end());
    int mx = atoi(x.c_str());
    x = to_string(mx - mn);
    if (x.size() != n)
      x = '0' + x;
    // cout << mx << " " <<  mx << " " << x << endl;
    for (int i = 0; i < tmp.size(); i++) {
      // cout << tmp[i] << " ";
      if (x == tmp[i]) {
        ans = i;
        flag = true;
        break;
      }
    }
    tmp.push_back(x);
    cnt++;
    if (flag == false)
      return rec(x);
    else
      return x;
  }
  return 0;
}
int main() {
  while (1) {
    cin >> s >> n;
    if (s == "0" && n == 0)
      break;
    cnt = 0;
    ans = 0;
    while (1) {
      while (s.size() != n)
        s = '0' + s;
      tmp.push_back(s);
      s = rec(s);
      cout << ans << " " << atoi(s.c_str()) << " " << cnt - ans << endl;
      tmp.clear();
    }
  }
  return 0;
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

void divide(char[], int, int);
int combine(char[], int);
void sort(char[], int, bool);
int power(int);

int main(void) {
  int in = -1, L, a[21];
  char val[6];

GET:
  cin >> in >> L;
  if (in != 0 || L != 0) {
    a[0] = in;
    for (int i = 1; i <= 20; i++) {
      divide(val, L, in);
      sort(val, L, true);
      int lrg = combine(val, L);
      sort(val, L, false);
      in = (a[i] = lrg - combine(val, L));

      for (int j = 0; j < i; j++)
        if (a[i] == a[j]) {
          cout << j << " " << a[j] << " " << i - j << endl;
          goto GET;
        }
    }
  }

  return 0;
}

void divide(char val[], int L, int num) {
  for (int i = 0; i < L; i++) {
    val[i] = num / power(L - i - 1);
    num -= val[i] * power(L - i - 1);
  }
}

int combine(char val[], int L) {
  int num = 0;
  for (int i = 0; i < L; i++)
    num += val[i] * power(L - i - 1);

  return num;
}

void sort(char val[], int L, bool down) {
  for (int i = 0; i < L - 1; i++)
    for (int j = 0; j < L - i - 1; j++)
      if ((down && val[j] < val[j + 1]) || !down && val[j] > val[j + 1]) {
        int tmp = val[j];
        val[j] = val[j + 1];
        val[j + 1] = tmp;
      }
}

int power(int n) {
  int num = 1;
  for (int i = 0; i < n; i++)
    num *= 10;

  return num;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {
  int val;
  while (cin >> val)
    cerr << val << endl;

  return 0;
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long int ll;
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
using namespace std;
int main(void) {
  string s;
  int n;
  while (cin >> s >> n && (n || s != "0")) {
    map<string, int> mp;
    for (int i = 1;; i++) {
      if (mp[s] == 0)
        mp[s] = i;
      else {
        cout << mp[s] - 1 << " " << s << " " << i - (mp[s]) << endl;
        break;
      }
      while (s.size() < n)
        s += "0";
      sort(s.begin(), s.end());
      ll mi = stol(s);
      sort(s.rbegin(), s.rend());
      ll ma = stol(s);

      ll next = ma - mi;
      s = to_string(next);
    }
  }
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  stoi


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(x) for (auto it = (x).begin(); it != (x).end(); it++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

using namespace std;

int main() {
  string s;
  int n;
  for (int i = 0; cin >> s >> n; i++) {
    vector<int> v;
    if (s == "0" && n == 0)
      break;
    rep(j, s.length(), 6) s.insert(s.begin(), '0');

    while (1) {
      rep(j, s.length(), 6) s.insert(s.begin(), '0');
      Sort(s);
      int max = stoi(s);
      sort(s.rbegin(), s.rend());
      int min = stoi(s);
      int diff = max - min;

      if (find(all(v), diff) != v.end()) {
        cout << find(all(v), diff) - v.begin() << " " << diff << " "
             << i - (find(all(v), diff) - v.begin()) << endl;
        break;
      } else
        v.pb(diff);

      s = to_string(diff);
    }
  }
  return 0;
}
Problem p00761 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

map<int, int> mapto;
string Next(string str, int l) {
  vector<int> lhs, rhs;
  FORIT(it, str) {
    lhs.push_back(*it - '0');
    rhs.push_back(*it - '0');
  }
  while ((int)lhs.size() < l) {
    lhs.push_back(0);
    rhs.push_back(0);
  }
  assert((int)lhs.size() == l);
  assert((int)rhs.size() == l);
  sort(lhs.rbegin(), lhs.rend());
  sort(rhs.begin(), rhs.end());
  int lv = 0;
  int rv = 0;
  REP(i, l) {
    lv = lv * 10 + lhs[i];
    rv = rv * 10 + rhs[i];
  }
  int ret = lv - rv;
  char s[100];
  sprintf(s, "%d", ret);
  return (string)s;
}

int main() {
  string str;
  int l;
  while (cin >> str >> l, l) {
    mapto.clear();
    REP(i, 100) {
      int v = atoi(str.c_str());
      if (mapto.count(v)) {
        printf("%d %d %d\\n", mapto[v], v, i - mapto[v]);
        break;
      }
      mapto[v] = i;
      str = Next(str, l);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

inline int MaxDay(int y, int m) {
  if (y % 3 == 0) {
    return 20;
  }
  return m % 2 == 1 ? 20 : 19;
}
int main() {
  int n;
  int y, m, d;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%d %d %d", &y, &m, &d);
    int ans = 0;
    while (y != 1000 || m != 1 || d != 1) {
      ans++;
      d++;
      if (d > MaxDay(y, m)) {
        d = 1;
        m++;
        if (m > 10) {
          m = 1;
          y++;
        }
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

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

// input
// a[0] L

// output
//    j where i is the smallest number that safisfies the condition
//            a[i] = a[j] (i > j)
//    a[i]
//    i - j

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> a;
  int L;
  while (true) {
    a.clear();
    a.resize(25);
    cin >> a[0] >> L;
    if (a[0] == "0" && L == 0)
      break;
    for (int _ = L - a[0].size(); _ > 0; _--) {
      a[0].push_back('0');
    }
    int i, j;
    bool found = false;
    i = 1;
    stringstream ss;
    for (; i < 25; i++) {
      string t(a[i - 1]);
      sort(t.begin(), t.end());
      string min_ai(t), max_ai(t);
      reverse(max_ai.begin(), max_ai.end());
      int min_n, max_n;
      ss.clear();
      ss << min_ai;
      ss >> min_n;
      ss.clear();
      ss << max_ai;
      ss >> max_n;
      ss.clear();
      ss << (max_n - min_n);
      ss >> a[i];
      for (int _ = L - a[i].size(); _ > 0; _--) {
        a[i].insert(0, 1, '0');
      }
      for (j = 0; j < i; j++) {
        if (a[j] == a[i]) {
          found = true;
          break;
        }
      }
      if (found)
        break;
    }
    int ans;
    ss.clear();
    ss << a[i];
    ss >> ans;
    cout << j << ' ' << ans << ' ' << (i - j) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

// input
// a[0] L

// output
//    j where i is the smallest number that safisfies the condition
//            a[i] = a[j] (i > j)
//    a[i]
//    i - j

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> a;
  int L;
  while (true) {
    a.clear();
    a.resize(20);
    cin >> a[0] >> L;
    if (a[0] == "0" && L == 0)
      break;
    for (int _ = L - a[0].size(); _ > 0; _--) {
      a[0].push_back('0');
    }
    int i, j;
    bool found = false;
    i = 1;
    stringstream ss;
    for (; i < 20; i++) {
      string t(a[i - 1]);
      sort(t.begin(), t.end());
      string min_ai(t), max_ai(t);
      reverse(max_ai.begin(), max_ai.end());
      int min_n, max_n;
      ss.clear();
      ss << min_ai;
      ss >> min_n;
      ss.clear();
      ss << max_ai;
      ss >> max_n;
      ss.clear();
      ss << (max_n - min_n);
      ss >> a[i];
      for (int _ = L - a[i].size(); _ > 0; _--) {
        a[i].insert(0, 1, '0');
      }
      for (j = 0; j < i; j++) {
        if (a[j] == a[i]) {
          found = true;
          break;
        }
      }
      if (found)
        break;
    }
    int ans;
    ss.clear();
    ss << a[i];
    ss >> ans;
    cout << j << ' ' << ans << ' ' << (i - j) << endl;
  }
  return 0;
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    int x = n;
    int a[30], b[30];
    a[0] = n;
    vector<int> v;
    while (x != 0) {
      v.push_back(x % 10);
      x /= 10;
    }
    int g = m - v.size();
    while (g--) {
      v.push_back(0);
    }
    sort(v.begin(), v.end());
    int k = 1;
    while (k) {
      int x = 0, y = 0;
      for (int i = 0; i < v.size(); i++) {
        x += v[i] * pow(10, v.size() - i - 1);
        y += v[v.size() - 1 - i] * pow(10, v.size() - i - 1);
      }
      a[k] = max(x, y) - min(x, y);
      b[k] = a[k];
      for (int i = 0; i < v.size(); i++) {
        v[i] = b[k] % 10;
        b[k] /= 10;
      }
      sort(v.begin(), v.end());
      k++;
      if (k > 20) {
        break;
      }
    }
    vector<pair<int, int>> vec;
    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 50; j++) {
        if (i <= j) {
          continue;
        }
        if (a[i] == a[j]) {
          vec.push_back(make_pair(i, j));
        }
      }
    }
    sort(vec.begin(), vec.end());
    cout << vec[0].second << " " << a[vec[0].first] << " "
         << vec[0].first - vec[0].second << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    int x = n;
    int a[30], b[30];
    a[0] = n;
    vector<int> v;
    while (x != 0) {
      v.push_back(x % 10);
      x /= 10;
    }
    for (int i = 0; i < m - v.size(); i++) {
      v.push_back(0);
    }
    sort(v.begin(), v.end());
    int k = 1;
    while (k) {
      int x = 0, y = 0;
      for (int i = 0; i < v.size(); i++) {
        x += v[i] * pow(10, v.size() - i - 1);
        y += v[v.size() - 1 - i] * pow(10, v.size() - i - 1);
      }
      a[k] = max(x, y) - min(x, y);
      b[k] = a[k];
      for (int i = 0; i < v.size(); i++) {
        v[i] = b[k] % 10;
        b[k] /= 10;
      }
      sort(v.begin(), v.end());
      k++;
      if (k > 25) {
        break;
      }
    }
    vector<pair<int, int>> c;
    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
        if (i <= j) {
          continue;
        }
        if (a[i] == a[j]) {
          c.push_back(make_pair(i, j));
        }
      }
    }
    sort(c.begin(), c.end());
    cout << c[0].second << " " << a[c[0].first] << " "
         << c[0].first - c[0].second << endl;
  }
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

string Hen(int N) {
  string S;
  char s;
  while (N != 0) {
    s = '0' - N % 10;
    S.insert(S.begin(), s);
    N /= 10;
  }
  return S;
}
int main() {
  string S, T[40];
  int L, N, M, j, len, flag;
  while (1) {
    cin >> S >> L;
    if (S[0] == '0' && L == 0)
      break;
    len = S.length();
    flag = 0;
    if (S.length() < L) {
      for (int i = 0; i < L - len; i++) {
        S.insert(S.begin(), '0');
      }
    }
    T[0] = S;
    for (int i = 1; i < 21; i++) {
      sort(S.begin(), S.end());
      N = atoi(S.c_str());
      reverse(S.begin(), S.end());
      M = atoi(S.c_str());
      S = Hen(N - M);
      len = S.length();
      if (S.length() < L) {
        for (int j = 0; j < L - len; j++) {
          S.insert(S.begin(), '0');
        }
      }
      T[i] = S;
    }
    for (int i = 1; i < 21; i++) {
      for (int j = 0; j < i; j++) {
        if (T[i] == T[j]) {
          cout << j << " " << atoi(T[i].c_str()) << " " << i - j << endl;
          flag = 1;
          break;
        }
      }
      if (flag == 1)
        break;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

string Hen(int N) {
  string S;
  char s;
  while (N != 0) {
    s = '0' - N % 10;
    S.insert(S.begin(), s);
    N /= 10;
  }
  return S;
}
int main() {
  string S, T[44];
  int L, N, M, j, len;
  while (1) {
    cin >> S >> L;
    if (S[0] == '0' && L == 0)
      break;
    len = S.length();
    if (S.length() < L) {
      for (int i = 0; i < L - len; i++) {
        S.insert(S.begin(), '0');
      }
    }
    T[0] = S;
    for (int i = 1; i < 21; i++) {
      sort(S.begin(), S.end());
      N = atoi(S.c_str());
      reverse(S.begin(), S.end());
      M = atoi(S.c_str());
      S = Hen(N - M);
      len = S.length();
      if (S.length() < L) {
        for (int j = 0; j < L - len; j++) {
          S.insert(S.begin(), '0');
        }
      }
      T[i] = S;
    }
    for (int i = 1; i < 21; i++) {
      for (j = i - 1; j >= 0; j--) {
        if (T[i] == T[j]) {
          cout << j << " " << atoi(T[i].c_str()) << " " << i - j << endl;
          break;
        }
      }
      if (T[i] == T[j])
        break;
    }
  }
  return 0;
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  vector<string> a(21);
  int l;
  while (cin >> a[0] >> l, l) {
    while (a[0].length() != l)
      a[0] = "0" + a[0];
    for (int i = 1; i < 21; i++) {
      string b = a[i - 1];
      int x = 0;
      sort(b.begin(), b.end(), greater<char>());
      x = stoi(b);
      sort(b.begin(), b.end());
      x -= stoi(b);
      a[i] = to_string(x);
      while (a[i].length() != l)
        a[i] = "0" + a[i];
    }
    int resi, resj;
    for (int i = 0; i < 21; i++)
      for (int j = 0; j < i; j++)
        if (a[i] == a[j]) {
          resi = i;
          resj = j;
          goto aa;
        }
  aa:
    cout << resj << " " << stoi(a[resj]) << " " << resi - resj << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  vector<string> a(20);
  int l;
  while (cin >> a[0] >> l, l) {
    for (int i = 0; i < l - int(a[0].size()); i++)
      a[0] = '0' + a[0];
    for (int i = 1; i < 20; i++) {
      string b = a[i - 1];
      int x = 0;
      sort(b.begin(), b.end(), greater<char>());
      x = stoi(b);
      sort(b.begin(), b.end());
      x -= stoi(b);
      a[i] = to_string(x);
      for (int j = 0; j < l - int(a[i].size()); j++)
        a[i] = '0' + a[i];
    }
    int resi, resj;
    for (int i = 0; i < 20; i++)
      for (int j = 0; j < i; j++)
        if (a[i] == a[j]) {
          resi = i;
          resj = j;
          goto aa;
        }

  aa:
    cout << resj << " " << stoi(a[resj]) << " " << resi - resj << endl;
  }

  return 0;
}