Language: C++

#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

string sort(string str, int len, int x){
    while(len != str.size()) str+='0';
    if(x == 1){
        rep(i,str.size() - 1){
            rep(j,str.size() - 1){
                if(str[j] > str[j + 1]){
                    char temp = str[j];
                    str[j] = str[j + 1];
                    str[j + 1] = temp;
                }
            }
        }
    }else if(x == 0){
        rep(i,str.size() - 1){
            rep(j,str.size() - 1){
                if(str[j] < str[j + 1]){
                    char temp = str[j];
                    str[j] = str[j + 1];
                    str[j + 1] = temp;
                }
            }
        }
    }
    return str;
}

int changeNum(string str){
    int num = 0, K = 1;
    for(int i = str.size() - 1; i >= 0; i--){
        num+=(str[i] - '0') * K;
        K*=10;
    }
    return num;
}

string changeStr(int n, int len){
    string str;
    int K = 1;
    rep(i,len) K*=10;
    rep(i,len + 1){
        if(n / K == 0) str+= '0';
        else str+= ('0' + n / K);
        n%=K; K/=10;
    }
    return str;
}

bool checkNum(int a[25], int i){
    rep(j,i){
        if(a[i] == a[j] && i != j){
            cout << j << ' ' << a[i] << ' ' << i - j << endl;
            return 1;
        }
    }
    return 0;
}

int main(){
    string str;
    int len, A[25];
    while(cin >> str >> len, str[0] != '0'||len){
        A[0] = changeNum(sort(str, len, -1));
        range(i,1,24){
            A[i] = changeNum(sort(str, len, 0)) - changeNum(sort(str,len, 1));
            str = changeStr(A[i], len - 1);
            if(checkNum(A, i)) break;
        }
    }
}

Compiler output:
a.cc:8:1: error: 'string' does not name a type
    8 | string sort(string str, int len, int x){
      | ^~~~~~
a.cc:34:15: error: 'string' was not declared in this scope
   34 | int changeNum(string str){
      |               ^~~~~~
a.cc:1:1: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
  +++ |+#include <string>
    1 | #define range(i,a,b) for(int i = (a); i < (b); i++)
a.cc:43:1: error: 'string' does not name a type
   43 | string changeStr(int n, int len){
      | ^~~~~~
a.cc: In function 'bool checkNum(int*, int)':
a.cc:58:13: error: 'cout' was not declared in this scope
   58 |             cout << j << ' ' << a[i] << ' ' << i - j << endl;
      |             ^~~~
a.cc:1:1: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | #define range(i,a,b) for(int i = (a); i < (b); i++)
a.cc:58:57: error: 'endl' was not declared in this scope
   58 |             cout << j << ' ' << a[i] << ' ' << i - j << endl;
      |                                                         ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | #define range(i,a,b) for(int i = (a); i < (b); i++)
a.cc: In function 'int main()':
a.cc:66:5: error: 'string' was not declared in this scope
   66 |     string str;
      |     ^~~~~~
a.cc:66:5: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
a.cc:68:11: error: 'cin' was not declared in this scope
   68 |     while(cin >> str >> len, str[0] != '0'||len){
      |           ^~~
a.cc:68:11: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:68:18: error: 'str' was not declared in this scope; did you mean 'std'?
   68 |     while(cin >> str >> len, str[0] != '0'||len){
      |                  ^~~
      |                  std
a.cc:69:26: error: 'sort' was not declared in this scope; did you mean 'short'?
   69 |         A[0] = changeNum(sort(str, len, -1));
      |                          ^~~~
      |                          short
a.cc:69:44: error: 'changeNum' cannot be used as a function
   69 |         A[0] = changeNum(sort(str, len, -1));
      |                                            ^
a.cc:71:47: error: 'changeNum' cannot be used as a function
   71 |             A[i] = changeNum(sort(str, len, 0)) - changeNum(sort(str,len, 1));
      |                                               ^
a.cc:71:77: error: 'changeNum' cannot be used as a function
   71 |             A[i] = changeNum(sort(str, len, 0)) - changeNum(sort(str,len, 1));
      |                                                                             ^
a.cc:72:19: error: 'changeStr' was not declared in this scope; did you mean 'changeNum'?
   72 |             str = changeStr(A[i], len - 1);
      |                   ^~~~~~~~~
      |                   changeNum