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<bits/stdc++.h>
using namespace std;

int main()
{
  int n;
  int l, s;
  vector<int> vi1;
  vector<int> vi2;
  int cnt;

  while(1){
    cin >> n;
    if(n == 0) break;

    cnt = 0;

    while(1){
      l = s = 0;
      vi1.clear();
      vi2.clear();
      if(n == 6174){
	cout << cnt << endl;
	break;
      }else if(n == 0){
	cout << "NA" << endl;
	break;
      }

      for(int i = 0;i < 4;++i){
	vi1.push_back(n % 10);
	vi2.push_back(n % 10);
	n /= 10;
      }
      
      //l,sを求める
      sort(vi1.begin(), vi1.end());
      sort(vi2.begin(), vi2.end(), greater<int>());

      /*
      cout << "vi1:";
      for(int i = 0;i < 4;++i){
	cout << vi1[i] << ' ';
      }
      cout << endl;
      cout << "vi2:";
      for(int i = 0;i < 4;++i){
	cout << vi2[i] << ' ';
      }
      cout << endl;
      */
      
      for(int i = 0;i < 4;++i){
	l *= 10;
	l += vi1[i];
	
	s *= 10;
	s += vi2[i];
      }

      /*
      cout << s << "-" << l << "=" << s - l << endl;
      */
      
      //新しいNを求める
      n = s - l;
      ++cnt;
    }

  }

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;

#define DEBUG(x) cerr << #x << " = " << x << endl

template<class T, class U> T lexical_cast(const U &from) {
    stringstream ss;
    ss << from;
    T to;
    ss >> to;
    return to;
}

bool same(const string &s) {
    return s[0] == s[1] && s[0] == s[2] && s[0] == s[3];
}

int main() {
    for(string S; cin >> S, S != "0000"; ) {
        if(same(S)) {
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(S != "6174") {
            sort(begin(S), end(S));
            string L = "    ";
            for(int i = 0; i < 4; i++) L[i] = S[3 - i];
            S = lexical_cast<string>(lexical_cast<int>(L) - lexical_cast<int>(S));
            ans++;
        }
        cout << ans << endl;
    }
}

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

C++: #include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n)  FOR(i, 0, n)

int main()
{
    int num;

    while (cin >> num, num) {
        if (!(num % 1111)) {
            cout << "NA" << endl;
        }
        else {
            int cnt = 0;
            while (num != 6174){
                int L = 0, S = 0, n[4];
                REP (i, 4) {
                    n[i] = num % 10;
                    num /= 10;
                }
                sort(n, n + 4);
                REP (i, 4) {
                    L = L * 10 + n[3 - i];
                    S = S * 10 + n[i];
                }
                cnt++;
                num = L - S;
            }
            cout << cnt << endl;
        }
    }
    return (0);
}

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

#define ALL(v) (v).begin(), (v).end()

int rec(string N, int cnt) {
  vector<char> LN(4), SN(4);
  int L = 0, S = 0;
  for (int i = 0; i < 4; i++) {
    LN[i] = SN[i] = N[i] - '0';
  }
  sort(ALL(LN));
  sort(ALL(SN));
  for (int i = 0; i < 4; i++) {
    L *= 10;
    S *= 10;
    L += LN[3 - i];
    S += SN[i];
  }
  return (L - S == 6174) ? cnt + 1 : rec(to_string(L - S), cnt + 1);
}

signed main()
{
  string N;
  while (cin >> N, N != "0000") {
    if (N[0] == N[1] and N[2] == N[3] and N[0] == N[2]) cout << ("NA");
    else if (N == "6174") cout << ("0");
    else cout << rec(N, 0);
    cout << "\n";
  }

  return (0);
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); }
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const int MOD = 1000000007;

using namespace std;
typedef vector<ll> vl;
typedef vector<vl> vvl;

int main() {
	string s;
	while (cin >> s) {
		if (s == "0000") {
			break;
		}
		int ans = 0;
		if (toInt(s) % 1111 == 0) {
			cout << "NA" << endl;
		}
		else {
			while (s != "6174") {
				string a = s, b = s;
				stable_sort(ALL(a));
				stable_sort(RALL(b));
				s = toStr(toInt(b) - toInt(a));
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int N,S = 0;
   while(1)
   {
       cin >> N;
       int big[4] = {0},small[4],ans = 0;
       if(N == 0000)break;
       if(N % 1111 == 0)
       {
           puts("NA");
       }
       else
       {
           while(N != 6174)
           {


               big[0] = small[0] = N / 1000;
               big[1] = small[1] = (N - (big[0]*1000)) / 100;
               big[2] = small[2] = (N - (big[0]*1000 + big[1]*100)) / 10;
               big[3] = small[3] = N % 10;
               /*for(int i = 0; i < 4; i++)
               {
                   cout << big[i] << "??¨" << small[i] << endl;
               }*/
               for(int i = 0; i < 4; i++)
               {
                   for(int j = i+1; j < 4; j++)
                   {
                       if(big[i] > big[j])swap(big[i],big[j]);
                       if(small[i] < small[j])swap(small[i],small[j]);
                   }
               }
               N = 0; S = 0;
               int aaa = 1;
               for(int i = 0; i < 4; i++)
               {

                   S += small[i]*aaa;
                   N += big[i]*aaa;
                   aaa = aaa*10;
               }
               N -= S;
               ans++;
           }
           cout << ans << endl;
       }

   }
}