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]
	return solve(l-s) + 1
}

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

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

int main(){
	int n,d[4],turn;
	while(1){
		A:
		cin >> n;
		if(n==0){
			return 0;
		}
		d[3] = n/1000;
		d[2] = n/100 - d[3]*10;
		d[1] = n/10 - d[3]*100 - d[2]*10;
		d[0] = n - d[3]*1000 -d[2]*100 -d[1]*10;
		if((d[0]==d[1])&&(d[1]==d[2])&&(d[2]==d[3])){
			cout << "NA\n";
			goto A;
		}
		turn=0;
		while(n!=6174){
			d[3] = n/1000;
			d[2] = n/100 - d[3]*10;
			d[1] = n/10 - d[3]*100 - d[2]*10;
			d[0] = n - d[3]*1000 -d[2]*100 -d[1]*10;
		
			int y;
			for(int i=1;i<4;i++){
				for(int j=1;j<5-i;j++){
					if(d[j-1]>d[j]){
						y = d[j-1];
						d[j-1] = d[j];
						d[j] = y;
					}
				}
			}
		
			n = (d[3]-d[0])*999 + (d[2]-d[1])*90;

			turn++;
		}
		cout << turn <<  "\n";
	}
return 0;
}

C++: #include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

#include<stdio.h>
int main (){
	int N[4],L,S,c,i,a;
	while(1){
		for(i=0;i!=4;i++){
			scanf("%1d",&N[i]);
		}
		if(N[0]==N[1]&&N[2]==N[3]&&N[1]==N[2]){
			if(N[1]==0)
				break;
			else
				puts("NA");
		}
		else{
			for(i=0;c!=6174;i++){
				for(S=0;S!=4;S++){
					for(a=S;a!=4;a++){
						if(N[S]<N[a]){
							c=N[a];
							N[a]=N[S];
							N[S]=c;
						}
					}
				}
				L=1000*N[0]+100*N[1]+10*N[2]+N[3];
				S=1000*N[3]+100*N[2]+10*N[1]+N[0];
				L-=S;
				c=L;
				N[0]=L/1000;
				L%=1000;
				N[1]=L/100;
				L%=100;
				N[2]=L/10;
				L%=10;
				N[3]=L;
			}
			printf("%d\n",i);
		}
	}
}

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

using namespace std;

int main(){
	int n, l, s, a;
	char c[4];
	while (1){
		int ans = 0;
		cin >> n;
		if (n == 0000){
			break;
		}
		a = n;
		while (a != 6174){
			for (int i = 0; i < 4; i++){
				c[i] = a % 10;
				a /= 10;
			}
			if (c[0] == c[1] && c[0] == c[2] && c[0] == c[3]){
				cout << "NA" << endl;
				break;
			}
			sort(c, c + 4);
			l = c[0] * 1000 + c[1] * 100 + c[2] * 10 + c[3];
			reverse(c, c + 4);
			s = c[0] * 1000 + c[1] * 100 + c[2] * 10 + c[3];
			a = s - l;
			ans++;
		}
		if (a == 6174){
			cout << ans << endl;
		}
	}
}

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

using namespace std;

int main(){
	while(1){
		int n,l,s;
		int cnt = 0;
		string tmp;

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

		while(n != 6174){
			tmp = to_string(n);

			sort(tmp.begin(),tmp.end());
			s = stoi(tmp);

			reverse(tmp.begin(),tmp.end());
			l = stoi(tmp);
			n = l - s;
			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

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

int main() {
	string s;
	while (cin >> s) {
		if (s == "0000")break;
		int n[4] = { s[0] - '0',s[1] - '0',s[2] - '0',s[3] - '0' };
		int ans = 0;
		if (n[0] == n[1] && n[2] == n[3] && n[1] == n[2]) {
			ans = -1;
		}
		else {
			while (1) {
				if (n[0] == 6 && n[1] == 1 && n[2] == 7 && n[3] == 4)break;
				sort(n, n + 4);
				int na[4];
				na[0] = n[3] - n[0];
				na[1] = n[2] - n[1];
				na[2] = n[1] - n[2];
				na[3] = n[0] - n[3];
				for (int i = 0; i < 4; i++)n[i] = na[i];
				if (n[3] < 0) {
					n[3] += 10;
					n[2] --;
				}
				if (n[2] < 0) {
					n[2] += 10;
					n[1]--;
				}
				if (n[1] < 0){
					n[1] += 10;
					n[0]--;
				}
				ans++;
			}
		}
		if (ans == -1)cout << "NA" << endl;
		else cout << ans << endl;
	}
	return 0;
}