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<iostream>
#include<algorithm>
#include<functional>
using namespace std;

int now[4],L[4],S[4];
int ans[10000] = {0},cor = 0,mode[10000];	//mode[i] : 1 = int,0 = char
char *ansc[10000];

void acpy(int *p,int *q,int size){
	for(int i = 0;i < size;i++)
		p[i] = q[i];
}
int main(){
	char str[6];
	int i;
	while(cin >> str){
		for(i = 0;i < 4;i++){
			if(str[i] != '0')
				break;
		}
		if(i == 4)
			break;
		//ここから開始
		for(i = 0;i < 3;i++){
			if(str[i] != str[i+1])
				break;
		}
		if(i == 3){
			ansc[cor] = "NA";
			cor++;
			continue;
		}
		for(i = 0;i < 4;i++){
			now[i] = str[i]-48;
		}
		while(now[0] != 6 || now[1] != 1 || now[2] != 7 || now[3] != 4){
			sort(now,now+4,greater<int>());
			acpy(L,now,4);
			sort(now,now+4);
			acpy(S,now,4);
			for(i = 3;i >= 0;i--){
				now[i] = L[i]-S[i];
				if(now[i] < 0){
					now[i] += 10;
					L[i-1]--;
				}
			}
			ans[cor]++;
		}
		mode[cor] = 1;
		cor++;
	}
	for(i = 0;i < cor;i++){
		if(mode[i])
			cout << ans[i] << endl;
		else
			cout << ansc[i] << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
	int a,s[5],d,f,g,h,i,j,z;
	while(1){
		scanf("%d",&a);
		if(a==0000) break;
		h=0;
		z=0;
		while(1){
			if(a==6174) break;
			s[0]=a/1000;
			s[1]=(a-s[0]*1000)/100;
			s[2]=(a-s[0]*1000-s[1]*100)/10;
			s[3]=(a-s[0]*1000-s[1]*100-s[2]*10);
		//	printf("%d %d %d %d\n",s[0],s[1],s[2],s[3]);
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(s[i]<s[j]){
						d=s[j];
						s[j]=s[i];
						s[i]=d;
					}
				}
			}
			a=s[0]*1000+s[1]*100+s[2]*10+s[3];
			f=s[3]*1000+s[2]*100+s[1]*10+s[0];
		//	printf("%d %d %d\n",a,f,a-f);
			if(a==f){
				z=1;
				break;
			}
			a-=f;
			h++;
		}
		if(z==1) printf("NA\n");
		else
			printf("%d\n",h);
	}
	return 0;
}

C++: #include<stdio.h>

int main(){

char ty;
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(1){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
	
	bool f=false;
	do{
	scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&ch5);
	ty=c1;
	
     if(ty==c1&&ty==c3&&ty==c4&&ty==c2)f=false;
	else f=true;

	if(ty=='0')f=true;
	if(f==false)printf("NA\n");

	}while(f!=true);
	if(c1=='0'&&c2=='0'&&c3=='0'&&c4=='0')break;
	
	
	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;
}

C++: #include <bits/stdc++.h>
#define endl    '\n'
using namespace std;

typedef long long ll;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int cnt;
    int n;
    int l[4], s[4];
    int l_num, s_num;

    cin >> n;

    while (n != 0) {
        cnt = 0;

        if (n % 1111 == 0) {
            cout << "NA";
        } else {
            while (n != 6174) {
                l_num = s_num = 0;

                for (int i = 3; i >= 0; --i) {
                    s[i] = n % 10;
                    n /= 10;
                }

                sort(s, s + 4);
                s_num += s[0] * 1000;
                s_num += s[1] * 100;
                s_num += s[2] * 10;
                s_num += s[3];

                l_num += s[3] * 1000;
                l_num += s[2] * 100;
                l_num += s[1] * 10;
                l_num += s[0];

                n = l_num - s_num;
                cnt++;
            }

            cout << cnt;
        }
        cout << endl;

        cin >> n;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int m,n,l;
  cin>>n;
  while(n!=0){
    int nn[4]={n%10,(n/10)%10,(n/100)%10,(n/1000)%10};
    if(nn[0]==nn[1]&&nn[1]==nn[2]&&nn[2]==nn[3]){
      cout<<"NA"<<endl;
    }else{
      int cou=0;
      while(nn[0]!=4&&nn[1]!=7&&nn[2]!=1&&nn[3]!=6){
	int rn[4];
	for(int i=0;i<3;i++){
	  for(int j=i+1;j<4;j++){
	    if(nn[i]>nn[j]){
	      int exi=nn[i];
	      nn[i]=nn[j];
	      nn[j]=exi;
	    }
	  }
	}
	for(int i=0;i<4;i++){
	  rn[i]=nn[i];
	}
	for(int i=0;i<4;i++){
	  if(nn[3-i]>nn[i]){
	    rn[i]+=10;
	    rn[i]-=nn[3-i];
	    rn[i+1]--;
	  }else{
	    rn[i]-=nn[3-i];
	  }
	}
	for(int i=0;i<4;i++){
	  nn[i]=rn[i];
	}
	cou++;
      }
      cout<<cou<<endl;
    }
    cin>>n;
  }
  return (0);
}

C++: #include<stdio.h>
int main(void)
{
	int n,r[4],l,s,x,y,z,a,i,j,flg;
	while(1){
		scanf("%d",&n);
		if(n==0000){
			break;
		}
		flg=1;
		a=0;
		while(n!=6174){
			y=0;
			x=1000;
			for(i=0;i<4;i++){
				r[i]=n/x;
				n=n%x;
				x=x/10;
				if(r[0]==r[i]) y++;
			}
			if(y==4) {
				flg=0;
				break;
			}
			l=0; s=0;
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(r[i]>r[j]){
						z=r[i];
						r[i]=r[j];
						r[j]=z;
					}
				}
			}
			printf("r==");
			for(i=0;i<4;i++) printf("%d",r[i]);
			printf("\n");
			x=1000;
			for(i=0;i<4;i++){
				s=r[i]*x+s;
				x=x/10;
			}
			printf("x=%d ",x);
			x=1;
			for(i=0;i<4;i++){
				l=r[i]*x+l;
				x=x*10;
			}
			n=l-s;
			a++;
		}
		if(flg==1) printf("%d\n",a);
		if(flg==0) printf("NA\n");
	}
	return 0;
}