Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		a := make([]int, 4)
		for i := 0; i < 4; i++ {
			a[i] = v % 10
			v /= 10
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] > a[j]
		})
		l := int(0)
		for i := 0; i < 4; i++ {
			l *= 10
			l += a[i]
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] < a[j]
		})
		s := int(0)
		for i := 0; i < 4; i++ {
			s *= 10
			s += a[i]
		}
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make([]int, 10000)
	for {
		var s string
		fmt.Scan(&s)
		v, _ = strconv.Atoi(s)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<bits/stdc++.h>
#include<string.h>
using namespace std;
signed main(){
  int n;
 
  while(cin>>n,n){
    int sum=0;
    int l[10]={};
    int s[10]={};
    
  while(true){
      if(n==6174){
	cout<<sum<<'\n';
	break;
      }
      for(int i=3;i>=0;i--){
	l[i]=n%10;
	s[i]=n%10;
	n/=10;
      }
      sort(s,s+4);
      sort(l,l+4,greater<int>());
      if(l[0]==s[0] && l[1]==s[1] && l[2]==s[2] && l[3]==s[3] && sum==0){
	cout<<"NA"<<'\n';
	break;
      }
      sum++;
      n=(l[0]*1000+l[1]*100+l[2]*10+l[3])-(s[0]*1000+s[1]*100+s[2]*10+s[3]);
    }
  }
  return(0);
}

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;
}