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 <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include<functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;



/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/



int main()
{
	for (char s[5]; scanf("%s", s), strcmp(s, "0000")!=0;)
	{
		if (count(s, s + 4, s[0]) == 4)
		{
			cout << "NA" << endl;
			continue;
		}
		
		int ans = 0;
		while (strcmp(s, "6174") != 0)
		{
			char s1[5];
			strcpy(s1, s);
			sort(s1, s1 + 4);
			char s2[5];
			strcpy(s2, s);
			sort(s2, s2 + 4, greater<char>());
			int a1 = atoi(s1);
			int a2 = atoi(s2);
			sprintf(s, "%04d", a2 - a1);
			ans++;
		}
		cout << ans << endl;
	}
	
	return 0;
}

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



//文字列を入れ替える変数
string f_max(string);

string f_min(string);

//減算関数
string sum(string , string);



int main(void)
{
	//入力変数
	string str_input;

	while (true)
	{
		cin >> str_input;

		if (str_input == "0000")
		{
			break;
		}
		int count = 0;
		for (int i = 1; i < 4; i++)
		{
			if (str_input[0] == str_input[i])
			{
				count++;
			}
		}
		if (count == 3)
		{
			cout << "NA" << endl;
		}
		else
		{
			//出力変数
			int output = 0;
			while (str_input != "6174")
			{
				str_input = f_max(str_input);
				//min変数
				string min = f_min(str_input);

				str_input = sum(str_input , min);

				output++;
			}

			cout << output << endl;
		}
	}

	return 0;
}



//文字列を入れ替える変数
string f_max(string max)
{
	//配列に格納
	int h_hozon[4] = {0};
	for (int i = 0; i < 4; i++)
	{
		h_hozon[i] = max[i];
	}

	//ソート
	for (int i = 0; i < 3; i++)
	{
		for (int j = i + 1; j < 4; j++)
		{
			if (h_hozon[i] < h_hozon[j])
			{
				int tmp = h_hozon[i];
				h_hozon[i] = h_hozon[j];
				h_hozon[j] = tmp;
			}
		}
	}

	for (int i = 0; i < 4; i++)
	{
		max[i] = h_hozon[i];
	}

	return max;
}

string f_min(string min)
{
	string h_tmp = "0000";

	for (int i = 0; i < 4; i++)
	{
		h_tmp[3 - i] = min[i];
	}

	return h_tmp;
}



//減算関数
string sum(string max , string min)
{
	int int_max = 0;
	int int_min = 0;

	//掛け算変数
	int kakeru = 1000;

	for (int i = 0; i < 4; i++)
	{
		int_max += ((max[i] - '0') * kakeru);
		int_min += ((min[i] - '0') * kakeru);

		kakeru /= 10;
	}

	int_max -= int_min;

	int waru = 1000;

	for (int i = 0; i < 4; i++)
	{
		max[i] = (int_max / waru) + '0';
		int_max %= waru;

		waru /= 10;
	}

	return max;
	
}

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

using namespace std;

int main() {
  string str;
  while(cin >> str) {
    if(str == "0000") break;
    int const size = str.size();
    
    bool ok = true;
    for(int i=0; i<size-1; i++) {
      ok = ok && str[i] == str[i+1];
    }
    if(ok) { cout << "NA" << endl; continue; }
    
    int cnt = 0;
    while(1) {
      if(str == "6174") {
        break;
      }
      string L = str, S = str;
      sort(S.begin(), S.end());
      sort(L.begin(), L.end(), greater<char>());
      const char *pL = L.c_str();
      const char *pS = S.c_str();
      char *e;
      int l = strtol(pL, &e, 10);
      int s = strtol(pS, &e, 10);
      int n = l - s;
      for(int i=0; i<size; i++) {
        str[size-i-1] = char(n%10 + '0');
        n/=10;
      }
      cnt ++;
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main(void)
{
	int n,i,j,a[4],l[4],s[4],cnt,kake,x,y,z;
	while(1){
		cnt=0;
		cin>>n;
		if(n==0000) break;
		for(i=3;i>=0;i--){
			a[i]=n%10;
			n=n/10;
		}
		for(i=0;i<4;i++){
			if(a[0]==a[i]) cnt=cnt+1;
		}
		if(cnt==4) cout<<"NA"<<endl;
		if(cnt!=4){
			cnt=0;
			while(1){
				if(a[0]==6&&a[1]==1&&a[2]==7&&a[3]==4) break;
				cnt=cnt+1;
				for(i=0;i<4;i++){
					l[i]=a[i];
					s[i]=a[i];
				}
				for(i=0;i<4;i++){
					for(j=0;j<4;j++){
						if(l[i]>l[j]){
							swap(l[i],l[j]);
						}
						if(s[i]<s[j]){
							swap(s[i],s[j]);
						}
					}
				}
/*				cout<<'l'<<endl;
				for(i=0;i<4;i++) cout<<l[i];
				cout<<endl;
				cout<<'s'<<endl;
				for(i=0;i<4;i++) cout<<s[i];
				cout<<endl;*/
				kake=1000;
				x=0;
				y=0;
				for(i=0;i<4;i++){
					x=x+l[i]*kake;
					y=y+s[i]*kake;
					kake=kake/10;
				}
				z=x-y;
//				cout<<x<<'|'<<y<<'|'<<z<<endl;
				for(i=3;i>=0;i--){
				a[i]=z%10;
				z=z/10;
				}
/*				cout<<'a'<<endl;
				for(i=0;i<4;i++) cout<<a[i];
				cout<<endl;
				if(cnt==2) break;*/
			}
			cout<<cnt<<endl;
		}
	}
/*	cout<<'a'<<endl;
	for(i=0;i<4;i++) cout<<a[i];
	cout<<endl;
	cout<<'l'<<endl;
	for(i=0;i<4;i++) cout<<l[i];
	cout<<endl;
	cout<<'s'<<endl;
	for(i=0;i<4;i++) cout<<s[i];
	cout<<endl;
	*/
	return 0;
}

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

int check(int* a){
  vector<int> b;
  for(int i=0; i<4; i++){
    b.push_back(*a%10);
    *a/=10;
  }
  sort(b.begin(),b.end());
  int p=0, q=0, u=1;
  for(int i=0; i<4; i++){
    q+=b[i]*u;
    p+=b[3-i]*u;
    u*=10;
  }
  return q-p;
}

int main(){
  int t;
  while(true){
    cin >> t;
    if(t==0)
      break;
    bool f = true;
    for(int i=1; i<9; i++)
      if(t==i*1111){
	cout << "NA\n";
	f = false;
      }
    if(f){
      if(t==6174)
	cout << 0 << endl;
      else{
	int kk = 1;
	int pp = check(&t);
	while(pp != 6174){
	  pp = check(&pp);
	  kk++;
	}
	cout << kk << endl;   
      }   
    }
  }
  return 0;
}

C++: #include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;
inline 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()
{
  string n;
  for(;cin>>n,n!="0000";)
    {
      int cnt=0;
      while(n!="6174")
	{
	  string s=n;
	  string l=n;
	  sort(s.begin(),s.end());
	  sort(l.begin(),l.end(),greater<char>());
	  n=toString(toInt(l)-toInt(s));
	  if(s==l)
	    {
	      cnt=-1;
	      break;
	    }  
	  cnt++;  
	}
      if(cnt>=0)
	cout<<cnt<<endl;
      else 
	cout<<"NA"<<endl;
    }
}

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