Go: package main

import (
	"fmt"
	"sort"
)

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 {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<sstream>
#include<string>
#include<iomanip>
using namespace std;
int cnv(string str,bool gFlag){
    sort(str.begin(),str.end());
    if(gFlag)reverse(str.begin(),str.end());
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
int main(){
    int n;
    while(cin>>n,n){
        int cnt=0,left=0;
        bool flag=false;
        while(n!=6174){
            stringstream ss;
            ss<<setw(4)<<setfill('0')<<n;
            string str;
            ss>>str;
            n=cnv(str,true)-cnv(str,false);
            cnt++;
            if(left==n){
                flag=true;
                break;
            }
            left=n;
            cout<<cnt<<":"<<n<<endl;
        }
        if(flag)cout<<"NA"<<endl;
        else cout<<cnt<<endl;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
bool NA(string s) {
	if (s == "1111" || s == "2222" || s == "3333" || s == "4444" || s == "5555" || s == "6666" || s == "7777" || s == "8888" || s == "9999")return true;
	else return false;
}
signed main() {
	string s;
	while (cin >> s, s != "0000") {
		int ans(0);
		while (s != "6174") {
			if (NA(s)) {
				cout << "NA" << endl;
				goto here;
			}
			string big(s), small(s);
			sort(all(small));
			sort(all(big), greater<char>());
			s = to_string(stoi(big) - stoi(small));
			while (s.size() < 4)s = "0" + s;
			ans++;
		}
		cout << ans << endl;
	here:;

	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n,i;

int solve(int num){

	if(num == 6174){
		return i;
	}

	i++;

	int k[5];
	k[0] = num/1000;
	k[1] = (num%1000)/100;
	k[2] = (num%100)/10;
	k[3] = num%10;

	sort(k,k+4);
	int s = k[0]*1000+k[1]*100+k[2]*10+k[3];

	reverse(k,k+4);
	int l = k[0]*1000+k[1]*100+k[2]*10+k[3];

	solve(l-s);

}



int main (){

	for(int d=0; d<10000; d++){
	
		i=0;

		cin >> n;
		if(n*1 == 0)break;

		if(n%1111 == 0){
			cout << "NA" << endl;
		}

		else{
			cout << solve(n) << endl;
	
		}
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
template<typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& a){return is >> a.first >> a.second; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1, T2>& a){ return os << a.first << " "<<a.second; }
template<typename T> istream& operator>>(istream& is, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) is >> vc[i]; return is; }
template<typename T> ostream& operator<<(ostream& os, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) os << vc[i] << endl; return os; }

main()
{
  string N;
  while(cin >> N, N != "0000") {
    if(count(N.begin(), N.end(), N[0]) == 4) {
      cout << "NA" << endl;
    } else {
      int ret = 0;
      for( ; N != "6174"; ret++) {
        string Sort1 = N, Sort2 = N;
        sort(Sort1.rbegin(), Sort1.rend());
        sort(Sort2.begin(), Sort2.end());
        int Value = atoi(Sort1.c_str()) - atoi(Sort2.c_str());
        stringstream sss; sss << Value;
        N = sss.str();
        while(N.size() < 4) N = "0" + N;
      }
      cout << ret << endl;
    }
  }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int main()
{
	string s;
	while(cin >> s)
	{
		if(s == "0000")
		{
			break;
		}

		int res = 0;
		if(s == "6174")
		{
			cout << res << endl;
			continue;
		}
		for(int i = 1; ;i++)
		{
			string UP = s, LOW = s;
			sort(LOW.begin(), LOW.end());
			sort(UP.begin(), UP.end(), greater<char>());

			int up = to_int(UP), low = to_int(LOW);
			int mid = up - low;

			if(mid == 0)
			{
				res = -1;
				break;
			}
			if(mid == 6174)
			{
				res = i;
				break;
			}
			s = to_str(mid);
			while(s.size() < 4)
			{
				s += '0';
			}
		}

		if(res == -1)
		{
			cout << "NA" << endl;
			continue;
		}
		cout << res << endl;
	}

}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int check(int n){
	int a[4];
	int s1=0,s2=0,s3;
	for(int i=0;i<4;i++){
		a[i]=n%10;
		n/=10;
	}
	for(int i=0;i<4;i++){
		for(int j=0;j<3;j++){
			if(a[j]<a[j+1])swap(a[j],a[j+1]);
		}
	}
	for(int i=0;i<4;i++){
		int j=3-i;
		s1*=10;   s2*=10;
		s1+=a[i]; s2+=a[j];
	}
	s3=s1-s2;
	return s3;
}
int s[10005];
int main(){
	int n;
	int i=0,j=0;
	while(cin>>n && n){
		for(int k=0;k<10000;k+=1111){
			if(n==k){
				s[i]=-1;
				break;
			}
		}
		if(s[i]==-1){
			i++;
			continue;
		}
		while(1){
			if(n==6174)break;
			j++;
			n=check(n);
		}
		s[i]=j;
		j=0;
		i++;
	}
	for(j=0;j<i;j++){
		if(s[j]==-1){
			cout<<"NA"<<endl;
		}else{
			cout<<s[j]<<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main(){
    int cnt,a,b;
    char num[5];
    while(1){
        cin >> num;
        cnt = 0;
        if(strcmp(num,"0000") == 0) break;
        if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
            cout << "NA" << endl;
            continue;
        }
        while(1){
            if(strcmp(num,"6174") == 0) break;
            cnt++;
            sort(num,num + 4);
            a = atoi(num);
            sort(num,num + 4,greater<char>());
            b = atoi(num);
            b -= a;
            sprintf(num,"%04d",b);
        }
        cout << cnt << endl;
    }
}