Go: package main

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

var memo []int8

func solve(v int) int8 {
	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([]int8, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<iostream>
using namespace std;
int main()
{
    int input = 0;
    int digit[4];
    int max, min;
    int base[4]={1000,100,10,1};
    int c=0,i,j,tmp;

    while(input != 6174)
    {
        cin >> input;
        if(input == 0) return 0;
        if(input % 1111 == 0){
	    cout << "NA" << endl;
	    return 0;
        }
        for(i=0; i<4; i++) // 各桁を取り出す
        {
            digit[i]= input / base[i];
            input = input % base[i]; // Highestを除く
        }
        for(i=0;i<3;i++) // sort
        {
            for(j=i+1;j<4;j++)
            {
                if(digit[i]<digit[j])
                {
                    tmp=digit[i];
                    digit[i]=digit[j];
                    digit[j]=tmp;
                }
            }
        }
	max = min = 0;
        for(i=0;i<4;i++)
        {
            max += (digit[i]*base[i]);
            min += (digit[i]*base[3-i]);
        }
        input = max-min;
        c++;
    }
    cout << c << endl;
    return 0;
}

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");
			scanf("%d",&n);
		return 0;
		}
		
		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 <math.h>

using namespace std;



int main(){

	int N;

	while (cin >> N){

		if (N == 0){

			break;

		}else if(N % 1111 == 0){
			cout << "NA\n";
			continue;
		}
		int cnt = 0;
		while(N != 6174){
			int n[4];
			for(int i = 0; i < 4; i++){
				n[i] = N / pow(10,3 - i);
				N %= (int)pow(10,3 - i);
			}
			sort(n,n + 4,greater<int>());
			int L = n[0]*1000 + n[1]*100 + n[2]*10 + n[3];
			sort(n,n + 4);
			int S = n[0]*1000 + n[1]*100 + n[2]*10 + n[3];
			N = L - S;
			cnt++;
		}
		cout << cnt << endl;

	}

	return 0;

}

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

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 <bits/stdc++.h>
using namespace std;
int main(void)
{	
	string n,n1,n2,n3,y3;
	string x1,y1,y2,g="1000";
	int x=0;
	int i,x2;
	int flg=0;
	int a[5],b[5],c[5];
	while(1){
		cin>>n;
		if(n=="0000"){
			break;
		}
		flg=0;
		x=0;
		while(1){
			n1="";
			n2="";
			n3="";
			x1="";
			y1="";
			y2="";
			y3="";
			
			for(i=0;i<4;i++){
				x1=n[i];
				a[i]=stoi(x1);
			}
			if(n=="6174"){
				break;
			}
			if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]){
				flg=1;
				break;
			}
			
			//一回目
			sort(a,a+4,greater<int>());
			/*for(i=0;i<4;i++){
				cout<<a[i];
			}
			cout<<endl;*/
			//x++;
			for(i=0;i<4;i++){
				b[i]=a[i];
				x2=a[i];
				y3=to_string(x2);
				n1+=y3;
			}
			if(n1=="6174"){
				break;
			}
			
			//二回目
			sort(a,a+4);
			/*for(i=0;i<4;i++){
				cout<<a[i];
			}*/
			//cout<<endl;
			//x++;
			for(i=0;i<4;i++){
				c[i]=a[i];
				x2=a[i];
				n2+=to_string(x2);
			}
			//cout<<n2<<endl;
			if(n2=="6174"){
				break;
			}
			//三回目
			//x++;
			for(i=0;i<4;i++){
				int f1=c[i];
				y1+=to_string(f1);
				int f2=b[i];
				y2+=to_string(f2);
			}
			//cout<<y1<<" "<<y2<<endl;
			int m;
			m=stoi(y1);
			int e;
			e=stoi(y2);
			int h;
			h=e-m;
			
			//cout<<m<<" "<<e<<endl;
			//cout<<h<<endl;
			n3=to_string(h);
			if(h<1000){
				n3="0"+n3;
			}
			//cout<<n3<<endl;
			x++;
			if(n3=="6174"){
				break;
			}
			n=n3;
			
		}
		if(flg==1){
			cout<<"NA"<<endl;
		}
		else{
			cout<<x<<endl;
		}
	}
	return 0;
}

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

int ans[10010];
char tmp[8];

int dfs(int x){
	if(ans[x] >= 0){
		return ans[x];
	}
	if(ans[x] != -1){
		return INT_MIN;
	}
	ans[x] = INT_MIN;

	sprintf(tmp, "%04d", x);
	sort(tmp, tmp + 4);
	int b = strtol(tmp, 0, 10);
	reverse(tmp, tmp + 4);
	int a = strtol(tmp, 0, 10);
	ans[x] = dfs(a - b) + 1;
	return ans[x];
}


int main(){
	for(int i = 0; i < 10005; ++i){
		ans[i] = -1;
	}
	ans[6174] = 0;

	int n;
	while(scanf("%d", &n), n){
		int a = dfs(n);
		printf(a < 0 ? "NA\n" : "%d\n", a);
	}
}

C++: #include <stdio.h>

int main(void)
{
	for(;;)
	{
		int n,a[4],b,c,d,f=0,l,s;
		scanf("%d",&n);
		if(n==0)
		{
			break;
		}
		else if(n%1111==0)
		{
			printf("NA\n");
		}
		else
		{
			for(;;)
			{
				if(n==6174)
				{
					break;
				}
				a[0]=n/1000;
				a[1]=n/100-a[0]*10;
				a[2]=n%100/10;
				a[3]=n%10;
				for(b=0;b<4;b++)
				{
					for(c=b+1;c<4;c++)
					{
						if(a[b]<a[c])
						{
							d=a[b];
							a[b]=a[c];
							a[c]=d;
						}
					}
				}
				l=(a[0]*1000+a[1]*100+a[2]*10+a[3]);
				s=(a[0]+a[1]*10+a[2]*100+a[3]*1000);
				n=l-s;
				f++;
			}
			printf("%d\n",f);
		}
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
using namespace std;
int main() {
	while (true) {
		int n[4], cnt;
		char wat[4];
		scanf("%s", &wat);
		if (wat[0] == '0'&&wat[1] == '0'&&wat[2] == '0'&&wat[3] == '0') { goto endnow; }
		for (int i = 0;i < 4;++i) {
			n[i] = (int) wat[i] - 48;
		}
		for (int i = 1;i < 10;++i) {
			if (n[0] == i&&n[1] == i&&n[2] == i&&n[3] == i) {
				printf("NA\n");
				goto Ihatemath;
			}
		}
		cnt = 0;
		while (true) {
			int l[4];
			if (n[0] == 6 && n[1] == 1 && n[2] == 7 && n[3] == 4) {
				printf("%d\n", cnt);
				goto Ihatemath;
			}
			memcpy(l, n, sizeof(n));
			sort(l, l + 4,greater<int>());
			int lon = l[0] * 1000 + l[1] * 100 + l[2] * 10 + l[3];
			int shor = l[0] + l[1] * 10 + l[2] * 100 + l[3] * 1000;
			int wanna = lon - shor;
			n[0] = wanna / 1000;
			wanna = wanna % 1000;
			n[1] = wanna / 100;
			wanna = wanna % 100;
			n[2] = wanna / 10;
			wanna = wanna % 10;
			n[3] = wanna;
			cnt++;
		}
	Ihatemath:;
	}
endnow:;
	return 0;
}