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<iostream>
#include<algorithm>
#include<functional>
#include<string>

int main() {
	std::string str;
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		std::string l, s;
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			l = str;
			std::sort(l.begin(), l.end(), std::greater<char>());
			s = str;
			std::sort(s.begin(), s.end());
			str = std::to_string(std::stoi(l) - std::stoi(s));
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

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

std::string intToStr( int n )
{
	std::ostringstream oss;
	oss << n;

	return oss.str();
}

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

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

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

	return 0;
}

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

int main()
{
	int n;

	cin >> n;

	while (n > 0) {
		int a[4];
		a[0] = n / 1000;
		a[1] = (n % 1000) / 100;
		a[2] = (n % 100) / 10;
		a[3] = n % 10;

		if (a[0] == a[1] && a[0] == a[2] && a[0] == a[3])
			cout << "NA" << endl;
		else {
			int t = 0;

			while (n != 6174) {
				a[0] = n / 1000;
				a[1] = (n % 1000) / 100;
				a[2] = (n % 100) / 10;
				a[3] = n % 10;
				sort(a, a + 4);
				n = (a[3] - a[0]) * 999 + (a[2] - a[1]) * 90;
				t++;
			}

			cout << t << endl;
		}

		cin >> n;
	}

	return 0;
}

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

	using namespace std;

	int main()
	{
		int m, n[4], mi[4], ma[4], na;
		int	max_int, min_int, n_int, ans;
		bool same = false;
			while (scanf("%1d", &n[i])){
				na = n[0];
				for (int i = 1; i < 4; i++){
					if (na != n[i]) same = true;
				}
				if (!same) cout << "NA" << endl;

			while(n[0] != 6 || n[1] != 1 || n[2] != 7 || n[3] != 4){
				if (!same) break;
				sort(n, n + 4);
				
				for (int i = 0; i < 4; i++){
					mi[i] = n[i];
					ma[3 - i] = n[i];
				}

				for (int i = 0; i < 4; i++){
					max_int = ma[i] * 10 ^ i;
					min_int = mi[i] * 10 ^ i;
				}
				n_int = max_int - min_int;
				for (int i = 0; i < 4; i++){
					n[i] = n_int / (10 ^ (3 - i)) % 10;
				}
				ans++;
			}

			if (!same) cout << ans << endl;
		}
		return (0);
	}

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


bool allsame(string s){
  char c=s[0];
  for(int i=0;i<s.size();i++){
    if(s[i]!=c)return 0;
  }
  return 1;
}

string toSortString(int n){
  string res="0000";
  int tmp[4]={0,0,0,0};
  for(int i=0;n!=0;i++){
    tmp[i]=n%10;
    n/=10;
  }
  for(int i=0;i<4;i++){
    res[i]=tmp[i]+'0';
  }
  sort(res.begin(),res.end());
  return res;
}

int make_l(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[i]-'0')*pow(double(10),i);
  }
  return res;
}

int make_s(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[s.size()-1-i]-'0')*pow(double(10),i);
  }
  return res;
}

int main(){
  int n;
  int ans=0;
  while(cin>>n,n!=0){
    ans=0;
    while(n!=6174){
      string tmp_n=toSortString(n);
      if(allsame(tmp_n)){
        cout<<"NA"<<endl;
        break;
      }
      ans++;
      int l,s;
      cout<<tmp_n<<endl;
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      cout<<l<<endl;
      cout<<s<<endl<<endl;
      n=l-s;
      //cout<<n<<endl;
      assert(ans<100);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (;;) {
            int n=sc.nextInt();
            if(n==0) {
                break;
            }
            Integer[] l=d(n);
            if(l[0]==l[1]&&l[1]==l[2]&&l[2]==l[3]) {
                System.out.println("NA");
                continue;
            }
            int c=0;
            while(n!=6174) {
                c++;
                l=d(n);
                Arrays.sort(l,Comparator.reverseOrder());
                int a=c(l);
                Arrays.sort(l);
                int b=c(l);
                n=a-b;
            }
            System.out.println(c);
        }
    }
    static int c(Integer[] l) {
        int n=0;
        for(int i=0,j=1000;i<4;i++,j/=10) {
            n+=l[i]*j;
        }
        return n;
    }
    static Integer[] d(int n) {
        Integer[] l=new Integer[4];
        for(int i=0,j=1000;i<4;i++,j/=10) {
            l[i]=n/j;
            n%=j;
        }
        return l;
    }
}

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

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)
using namespace std;

int to[10000];
int memo[10000];

void init(){
	string str, rstr;
	int L,S;
	stringstream ss;
	rep(i,10000)memo[i] = -1;
	rep(i,10000){
		ss.clear();
		ss<<i;
		ss>>rstr;
		sort(rstr.begin(),rstr.end());
		str = rstr;
		reverse(str.begin(), str.end());
		ss.clear();
		ss << (str + " " + rstr);
		ss >> L >> S;
		to[i] = L-S;
	}
}

int main(void){
	int n;
	init();
	
	while(scanf("%d", &n),n){
		int ans = 0;
		int n_ = n;
		if(to[n] == 0){
			puts("NA");
			continue;
		}
		if(memo[n] != -1){
			ans = memo[n];
		}else{
			while(n != 6174){
				n = to[n];
				ans++;
			}
			memo[n_] = ans;
		}
		printf("%d\n", ans);
	}
	
	return 0;
}

C++: #include<stdio.h>

int main(){

char c1,c2,c3,c4,ch5;
int n1=0,n2=0,n3=0,n4=0,N=0,S=0,P=0,K=0;
int n[5]={};

while(scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&ch5)!=EOF){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
  for(int i=0;i<4;i++)
	n[i]=0;
int cnt=0;


n[0]=c1-'0';n[1]=c2-'0';n[2]=c3-'0';n[3]=c4-'0';


for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}

N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

P=N-S;
if(P!=6174)cnt++;

while(P!=6174){
N=P;
K=P;n[0]=N/1000;
K=N%1000;n[1]=K/100;
K=N%100;n[2]=K/10;
K=N%10;n[3]=K;

//printf(" %d\n",N);

for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}


N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

cnt++;
P=N-S;

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