Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		str := ([]byte)(strconv.Itoa(v))
		sort.Slice(str, func(i, j int) bool {
			return str[i] < str[j]
		})
		s, _ := strconv.Atoi(string(str))
		sort.Slice(str, func(i, j int) bool {
			return str[i] > str[j]
		})
		l, _ := strconv.Atoi(string(str))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make(map[int]int)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n)n){
        int cnt = 0;
        bool flg = 0;
        while(1){
            if(n == 6174)break;
            if(n%1111 == 0){
                flg = 1;
                break;
            }
            int a[4]={0};
            a[0] = n/1000;
            a[1] = (n%1000)/100;
            a[2] = (n%100)/10;
            a[3] = n%10;
            int L[4],S[4];
            sort(a,a+4);
            for(int i = 0; i < 4; i++){
                L[3-i] = S[i] = a[i];
            }
            int l=0,s=0;
            for(int i = 0; i < 4; i++){
                l += L[3-i] * pow(10,i);
                s += S[3-i] * pow(10,i);
            }
            n = l - s;
            cnt++;
        }
        if(flg)puts("NA");
        else printf("%d\n",cnt);
    }
	return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
     int n,d[4],max,min,w[4]={1000,100,10,1};
     int c=0,i,j,tmp;
     cin >>n;
     while(n!=6174)
     {
       for(i=0;i<4;i++)
       {
             d[i]=n/w[i];
             n=n%w[i];
       }
       for(i=0;i<3;i++)
       {
          for(j=i+1;j<4;j++)
           {
                if(d[i]<d[j])
                 {
                    tmp=d[i];
                    d[i]=d[j];
                    d[j]=tmp;
                  }
            }
       }
        for(i=0;i<4;i++)
         {
              max=max+(d[i]*w[i]);
              min=min+(d[i]*w[3-i]);
         }
         n = max-min;
         c++;
     }
     cout << n << endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define ll2(x, y) ll x, y; cin >> x >> y;
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

int solve(int n, int cnt){
  if(n == 6174)return cnt;
  int a[4] = {n % 10, (n / 10) % 10, (n / 100) % 10, n / 1000};
  sort(a, a + 4);
  int mi = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
  int ma = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
  return solve(ma - mi, cnt + 1);
}

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int(n);
    if(!n)break;
    if(n % 1111 == 0)puts("NA");
    else pri(solve(n, 0));

//*/

//*/ break;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <stdlib>

using namespace std;

int main(){

	while(1){
		int n=0,l,s;
		int cnt = 0;
		string tmp;

		cin >> tmp;
		n = atoi(tmp.c_str());

		if(n == 0)break;
		if(n % 1111 == 0){
			printf("NA\n");
			continue;
		}

		while(n != 6174){
			sort(tmp.begin(),tmp.end());
			s = atoi(tmp.c_str());

			sort(tmp.begin(),tmp.end(),greater<char>());
			l = atoi(tmp.c_str());
			n = l - s;

			for(int i=0,p=1000;i<4;i++,p/=10){
				tmp[i] = '0' + n/p;
				n %= p;
			}
			n = atoi(tmp.c_str());
			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

C++: #include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int digs[5];
char c[5];
string str;

bool comp(const int &a, const int &b) {
    return a > b;
}

int sortDigit(int num) {
    int idx = 0;
    while (num != 0) {
        digs[idx++] = num % 10;
        num /= 10;
    }
    sort(digs, digs+idx, comp);
    int sdigit = 0, p = 1;
    for (int i = idx-1; i >= 0; i--) {
        sdigit += p * digs[i];
        p*=10;
    }
    while (sdigit < 1000) sdigit *= 10;
    return sdigit;
}

int revNum(int num) {
    int revnum = 0;
    while (num != 0) {
        revnum = revnum * 10 + (num % 10);
        num /= 10;
    }
    return revnum;
}

bool isSameDig(int num) {
    int dig = num % 10;
    num /= 10;
    while (num != 0) {
        if (dig != num % 10) return false;
        num /= 10;
    }
    return true;
}

int main(){
    int base = 6174;
    int prev, L, S, steps = 0;
    for (; ;) {
        scanf("%d", &prev);
        if (prev == 0) break;
        steps = 0;
        bool flag = false;
        while (prev != base) {
            L = sortDigit(prev); S = revNum(L);
            if (isSameDig(L)) { flag = true; break; }
            prev = L - S;
            steps++;
        }
        if (!flag) printf("%d\n", steps);
        else printf("NA\n");
    }
    return 0;
}

C++: #include <stdio.h>
int main(void){
	int i,s,o,n[4],q[4],toread,L,S,A=0,a,b,c;
	for(i=0;i<=3;i++){
		scanf("%1d",&n[i]);
		q[i]=n[i];
	}
	if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3]){
		printf("NA\n");
	}
	if(n[0]==6&&n[1]==1&&n[2]==7&&n[3]==4){
		printf("1\n");
	}
	else{
		for(s=0;A!=6174;s++){
		for(o=0;o<=3;o++){
			for(i=0;i<=3;i++){
				if(n[i]<n[o]){
					toread=n[i];
					n[i]=n[o];
					n[o]=toread;
				}
			}
		}
		L=n[0]*1000+n[1]*100+n[2]*10+n[3];
		for(o=0;o<=3;o++){
			for(i=0;i<=3;i++){
				if(n[i]>n[o]){
					toread=n[i];
					n[i]=n[o];
					n[o]=toread;
				}
			}
		}
		S=n[0]*1000+n[1]*100+n[2]*10+n[3];
		A=L-S;
		a=A/1000;
		n[0]=a;
		n[1]=(A-(n[0]*1000))/100;
		n[2]=(A-((n[0]*1000)+(n[1]*100)))/10;
		n[3]=A-((n[0]*1000)+(n[1]*100)+(n[2]*10));
		}
	printf("%d\n",s);
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
	int i,n,L[5],N[5],S[5],num=0;
	for(i=0;i<=4;++i){
		N[i]=0;
		L[i]=0;
		S[i]=0;
	}
	while(cin>>n,n){
		while(1){
			if (n==6174){
				cout<<"0"<<endl;
				break;
			}
			L[4]=N[4]=n/1000;
			L[3]=N[3]=n/100-N[4]*10;
			L[2]=N[2]=n/10-N[4]*100-N[3]*10;
			L[1]=N[1]=n-(n/10)*10;
			if (L[1]==L[2] && L[2]==L[3] && L[3]==L[4]){
				cout<<"NA"<<endl;
				break;
			}
			for(int j=1;j<=4;++j){
				for(i=4;i>=2;--i){
					if (L[i]<L[i-1]){
						L[0]=L[i];
						L[i]=L[i-1];
						L[i-1]=L[0];
					}
				}
			}
			for(int j=1,i=4;j<=4,i>=1;++j,--i){
					S[j]=L[i];
			}
			L[0]=L[4]*1000+L[3]*100+L[2]*10+L[1];
			S[0]=S[4]*1000+S[3]*100+S[2]*10+S[1];
			n=L[0]-S[0];
			num+=1;
			if (n==6174){
				cout<<num<<endl;
				num=0;
				break;
			}
		}
	}
	return 0;
}