Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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([]int, 10000)
	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;

string cn;
int main()
{
  while(true){
    cin >> cn;
    if(cn == "0000"){
      break;
    }
    if(cn[0] == cn[1] == cn[2] == cn[3]){
      cout << "NA" << endl;
    }

    else{
      int ans = 0;
      while(cn != "6174"){
	int in, intss;
	stringstream ss1, ss2, ss3;
	string cncp;
	sort(cn.begin(), cn.end(), greater<char>());
	ss1 << cn;
	ss1 >> intss;
	in = intss;
	for(int j = 0; j < 4; j++){
	  cncp += cn[3 - j];
	}
	ss2 << cncp;
	ss2 >> intss;
	in -= intss;
	ss3 << in;
	ss3 >> cn;
	ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
	int N,T,C;
	int L, S;
	int data[4];
	int su[4] = { 1000,100,10,1 };
	while (1) {
		C = 0;
		cin >> N;
		if (N == 0) break;
		if (N % 1111 == 0) {
			cout << "NA" << endl;
			continue;
		}

		while (N != 6174) {
			L = S = 0;
			C++;
			data[0] = N / 1000;
			N %= 1000;
			data[1] = N / 100;
			N %= 100;
			data[2] = N / 10;
			N %= 10;
			data[3] = N;

			for (int i = 0; i < 3; i++) {
				for (int j = i + 1; j < 4; j++) {
					if (data[i] < data[j]) {
						T = data[i];
						data[i] = data[j];
						data[j] = T;
					}
				}
			}
			
			for (int i = 0; i < 4; i++) {
				L += data[i] * su[i];
				S += data[3 - i] * su[i];
			}
	
			N = L - S;
		}
		cout << C << endl;
	}
}

C++: #include <iostream>
//#include <cstdio>
#include <stdio.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;



/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/



int main()
{
	for (char s[5]; scanf("%s", s), strcmp(s, "0000")!=0;)
	{
		if (count(s, s + 4, s[0]) == 4)
		{
			cout << "NA" << endl;
			continue;
		}
		
		int ans = 0;
		while (strcmp(s, "6174") != 0)
		{
			char s1[5];
			strcpy(s1, s);
			sort(s1, s1 + 4);
			char s2[5];
			strcpy(s2, s);
			sort(s2, s2 + 4, greater<char>());
			int a1 = atoi(s1);
			int a2 = atoi(s2);
			sprintf(s, "%04d", a2 - a1);
			ans++;
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<ll,ll> P;
const ll MOD=1000000007;
const ll INF=1000000010;
const ll LINF=4000000000000000010LL;
const int MAX=310;
const double EPS=1e-9;
int main(){
    int n;
    while(cin>>n){
        if((n%10000)/1000==(n%1000)/100&&(n%1000)/100==(n%100)/10&&(n%100)/10==n%10){
            if(n%10==0){
                break;
            }
            cout<<"NA"<<endl;
            continue;
        }
    int c=0;
    while(n!=6174){
        int a[4];
        a[0]=(n%10000)/1000;
        a[1]=(n%1000)/100;
        a[2]=(n%100)/10;
        a[3]=n%10;
        int p;
        sort(a,a+4);
        p=1;
        int l=0,s=0;
        for(int i=0;i<4;i++){
            s+=a[i]*p;
            p*=10;
        }
        p=1;
        for(int i=3;i>=0;i--){
            l+=a[i]*p;
            p*=10;
        }
        n=s-l;
        c++;
    }
    cout<<c<<endl;
    }
}

C++: #include <cstdio>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <functional>
using namespace std;

int main(){
	int n,nl,nh;
	char c[5],bl[5],bh[5];

	while(scanf("%s",c), (c[0] | c[1] | c[2] | c[3]) != '0'){
		if(c[0] == c[1] && c[0] == c[2] && c[0] == c[3]){
			printf("NA\n");
			continue;
		}
		int tm = 0;
		while(!(c[0] == '6' && c[1] == '1' && c[2] == '7' && c[3] == '4')){
			strcpy(bl,c);
			strcpy(bh,c);
			sort(bl,bl+4);
			sort(bh,bh+4, greater<char>());
			nl = nh = 0;
			for(int i = 0; i < 4; i++){
				nl += (bl[i] - '0') * pow(10,3-i);
				nh += (bh[i] - '0') * pow(10,3-i);
			}
			n = nh - nl;
			for(int i = 0; i < 4; i++){
				c[3-i] = '0' + (n % 10);
				n /= 10;
			}
			tm++;
		}
		printf("%d\n",tm);
	}

	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,"%d",b);
        }
        cout << cnt << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
string S; int a[4], b, cnt;
int main() {
	while (true) {
		cin >> S;
		if (S == "0000") { break; }
		if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) { cout << "NA" << endl; }
		else {
			b = stoi(S);
			cnt = 0;
			while (b != 6174) {
				cnt++;
				for (int i = 0; i < 4; i++) {
					a[i] = (b / (int)pow(10, i)) % 10;
				}
				sort(a, a + 4);
				b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 + (a[0] + a[3]);
			}
			cout << cnt << endl;
		}
	}
}

C++: #include<iostream>

using namespace std;

bool func_a(int *N, int *count);

bool main()
{
	int N = 0;
	int count = 0;
	bool flag = true;

	cin >> N;
	
	if(N == 6174)
	{
		cout << 0 << endl;
		flag = false;
	}

	for(int i = 0; i <= 9; i++)
	{
		if(N == i + i * 10 + i * 100 + i * 1000)
		{
			cout << "NA" << endl; 
			flag = false;
		}
	}
	
	if(flag)
	{
		while( func_a(&N, &count) ){};

		cout << count << endl;
	}

	return false;
}

bool func_a(int *N, int *count)
{	
	int num = *N;
	int Nmax[4];
	int Nmin[4];
	int L = 0, S = 0;

	*count += 1;

	for(int i = 0; i < 4; i++)
	{
		Nmax[i] = Nmin[i] = num % 10;
		num /= 10;
	}

	for(int i = 0; i < 4; i++)
	{
		for(int j = 0; j < 4; j++)
		{
			if(Nmax[i] < Nmax[j])
			{
				int tmp = Nmax[i];
				Nmax[i] = Nmax[j];
				Nmax[j] = tmp;
			}
			if(Nmin[i] > Nmin[j])
			{
				int tmp = Nmin[i];
				Nmin[i] = Nmin[j];
				Nmin[j] = tmp;
			}
		}
	}

	int dz[4] = {1, 10, 100, 1000};

	for(int i= 0; i < 4; i++)
	{
		L += Nmax[i] * dz[i];
		S += Nmin[i] * dz[i];
	}

	*N = L - S;

	if(*N == 6174)
	{
		return false;
	}
	else
	{
		return true;
	}
}

C++: #include <iostream>
#include <algorithm> 
using namespace std;
int main(){
	int N; 
	int n[4];
	while(true){
		int cnt=0;
		cin >> N;
		if(N==0) break;
		if(N%1111==0) {
			cout << "NA\n";
			continue;
		}
		while(N!=6174){
			n[0]=N/1000;
			n[1]=(N/100)%10;
			n[2]=(N/10)%10;
			n[3]=N%10;
			sort(n,n+4);
			int L = n[3]*1000+n[2]*100+n[1]*10+n[0];
			int S = n[0]*1000+n[1]*100+n[2]*10+n[3];
			N=L-S;
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}