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<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int num;
        cin>>num;
        if(num==0)break;
        int numnum[4];
        int ans=0;
        while(1){
            if(num==6174){
                cout<<ans<<endl;
                break;
            }
            numnum[0]=num%10000/1000;
            numnum[1]=num%1000/100;
            numnum[2]=num%100/10;
            numnum[3]=num%10/1;
            if(numnum[0]==numnum[1]&&numnum[0]==numnum[2]&&numnum[0]==numnum[3]){
                puts("NA");
                break;
            }
            int nummax[4];
            int nummin[4];
            for(int i=0;i<4;i++){
                nummax[i]=numnum[i];
                nummin[i]=numnum[i];
            }
            sort(nummax,nummax+4);
            sort(nummin,nummin+4);
            swap(nummax[0],nummax[3]);swap(nummax[1],nummax[2]);
            int mx=nummax[0]*1000+nummax[1]*100+nummax[2]*10+nummax[3]*1;
            int mn=nummin[0]*1000+nummin[1]*100+nummin[2]*10+nummin[3]*1;
            //printf("[%d][%d]\n",mx,mn);
            num=mx-mn;
            ans++;
        }
    }
    return 0;
}

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

// int型からstring型に変換する(非負整数)
string to_s(int n){
	string s;
	do{
		s.push_back( (n%10)+'0' );
		n /= 10;
	}while( n != 0 );
	reverse( s.begin() , s.end() );
	return s;
}

// string型からint型に変換する(非負整数)
int to_i(string s){
	int n = 0;
	for(int i=0 ; i < s.size() ; i++ ){
		n = (n * 10) + s[i] - '0';
	}
	return n;
}

int main(){
	string N;
	while( cin >> N, N != "0000" ){
		if( N[0] == N[1] && N[1] == N[2] && N[2] == N[3] ){
			cout << "NA" << endl;
		}else{
			int ans = 0;
			while( N != "6174" ){
				string L = N, S = N;
				sort(L.rbegin(), L.rend());
				sort(S.begin(), S.end());
				N = to_s(to_i(L) - to_i(S));
				while( N.size() < 4 ) N = "0" + N;
				ans++;
			}
			cout << ans << endl;
		}
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <sstream>

using namespace std;

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

int main(void)
{
	int a;
	while (cin >> a, a){
		string n = toString(a);
		if (n.length() < 4) {
			for (int i = 0; i <= 4-n.length(); i++) {
				n += '0';
			}
		}
		if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		int tmp;
		string N = n;
		while (1){
			if (N == "6174"){
				cout << cnt << endl;
				break;
			}
			//cout << N << endl;
			//cin >> tmp;
			string L = N; sort(L.rbegin(), L.rend());
			string S = N; sort(S.begin(), S.end());
			int d = toInt(L) - toInt(S);
			N = toString(d);
			if (N.length() < 4) {
			for (int i = 0; i <= 4-N.length(); i++) {
				N += '0';
			}
		}
			cnt++;
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

void st(char *hoge){
	for(int i=0;i<4;i++){
		for(int j=i+1;j<4;j++){
			if(hoge[i] < hoge[j]){
				char t = hoge[i];
				hoge[i] = hoge[j];
				hoge[j] = t;
			}
		}
	}
}

void rst(char *hoge){
	for(int i=0;i<4;i++){
		for(int j=i+1;j<4;j++){
			if(hoge[i] > hoge[j]){
				char t = hoge[i];
				hoge[i] = hoge[j];
				hoge[j] = t;
			}
		}
	}
}

int main(){

	while(1){
		int n=0,l,s;
		int cnt = 0;
		char tmp[5] = {0};

		scanf("%s",tmp);
		n = atoi(tmp);

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

		while(n != 6174){
			sprintf(tmp, "%d", n);
			rst(tmp);
			s = atoi(tmp);

			st(tmp);
			l = atoi(tmp);
			n = l - s;

			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

C++: #include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
char B[9];
#define I(s)strtol(s.c_str(),NULL,10)
string S(int i,int L){sprintf(B,"%0*d",L,i);return B;}
int main(){
	int a,i;
	for(;scanf("%d",&a),a;printf(a?"%d\n":"NA\n",i)){
		for(i=0;a&&a!=6174;i++){
			string t=S(a,4),s;
			sort(t.begin(),t.end());
			s=t;
			reverse(s.begin(),s.end());
			a=I(s)-I(t);			
		}
	}
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int l,s;
	int i,j,temp;
	int keta[4];
	int cnt;
	
	scanf("%d",&n);
	
	while(n!=0000){
		if(n%1111==0){
			printf("NA\n");
		}
		else {
			cnt=0;	
			while(n!=6174){	
				for(i=0;i<4;i++){
					keta[i]=n%10;
					n=n/10;
				}
				for(i=0;i<3;i++){
					for(j=i+1;j<4;j++){
						if(keta[i]<keta[j]){
							temp=keta[i];
							keta[i]=keta[j];
							keta[j]=temp;
						}
					}
				}
				l=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];

				for(i=0;i<3;i++){
					for(j=i+1;j<4;j++){
						if(keta[i]>keta[j]){
							temp=keta[i];
							keta[i]=keta[j];
							keta[j]=temp;
						}
					}
				}
				s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
				n=l-s;
				cnt++;
			}
	printf("%d\n",cnt);
		}
	scanf("%d",&n);
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
#include<iomanip>
using namespace std;
int latte(string str){
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
struct data{
    string str;
    int n;
    data(string a,int b):str(a),n(b){}
    data(){}
};
int main(){
    string str="";
    while(cin>>setw(4)>>setfill('0')>>str,str!="0000"){
        int memo[10000];
        fill(memo,memo+10000,-1);
        queue<data>Q;
        Q.push(data(str,0));
        while(!Q.empty()){
            data ins=Q.front();Q.pop();
            str=ins.str;int n=ins.n;
            if(memo[latte(str)]!=-1)continue;
            memo[latte(str)]=n;
            string a,b;
            a=b=str;
            sort(a.rbegin(),a.rend());sort(b.begin(),b.end());
            int to=latte(a)-latte(b);
            stringstream ss;
            ss<<to;
            string to_;
            ss>>to_;
            Q.push(data(to_,n+1));
        }
        if(memo[6174]==-1)cout<<"NA"<<endl;
        else cout<<memo[6174]<<endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	char str[5];
	sprintf( str, "%04d", n );

	return str;
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		bool fl = true;

		rep( i, 3 )
			fl &= L[i+1] == L[i];
		if( fl )
		{
			std::cout << "NA" << std::endl;
			continue;
		}

		int ans = 0;
		while( L != "6174" )
		{
			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		std::cout << ans << std::endl;
	}

	return 0;
}