Go: package main

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

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

func main() {
	var v int
	memo = make([]int, 10000)
	for {
		var s string
		fmt.Scan(&s)
		v, _ = strconv.Atoi(s)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

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

int main(){
	int n,nn;
	while(cin >> n,n){
		if(n % 1111 == 0) {
			cout << "NA" << endl;
		}else{
			int i = 0;
			for(;n != 6174;i++){
				int num[4] = {n / 1000,n / 100 % 10,n / 10 % 10,n % 10};
				sort(num,num + 4,greater<int>());
				n = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
				sort(num,num + 4);
				nn = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
				n = n - nn;
			}
			cout << i << endl;
		}
	}
	return 0;
}

C++: #include<stdio.h>

int main(){
  int i,n,l[4],s[4];

  scanf("%d",&n);

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

    for(i=3;i>=0;i--){
      int aa=a%10;
      l[i]=aa;
      a /= 10;
    }

    if(n == 6174){
      printf("0\n");
    }else if(l[0] == l[1] && l[0] == l[2] && l[0] == l[3]){
      printf("NA\n");
    }else{
     
      while(n != 6174){
	int len1,len2;
	for(len1=0;len1<3;len1++){
	  for(len2=0;len2<3-len1;len2++){
	    if(l[len2] < l[len2+1]){
	      int temp = l[len2];
	      l[len2] = l[len2+1];
	      l[len2+1] = temp;
	    }
	  }
	}   
	
	int j=0;
	for(i=3;i>=0;i--){
	  s[j]=l[i];
	  j++;
	}
	int ll=0,ss=0,ba=1000;
	for(i=0;i<4;i++){
	  ll += l[i]*ba;
	  ss += s[i]*ba;
	  ba /= 10;
	}
	n = ll - ss;
	cnt++;
	a=n;
	for(i=3;i>=0;i--){
	  int aa=a%10;
	  l[i]=aa;
	  a /= 10;
	}
      }
      printf("%d\n",cnt);
    }
    scanf("%d",&n);
  }
  return 0;
}

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

int to_i(const string &s) {
    int n;
    istringstream is(s);
    is >> n;
    return n;
}

string to_s(int n) {
    char buf[256];
    sprintf(buf, "%04d", n);
    return string(buf);
}

int main() {
    string s;
    while (cin >> s, s != "0000") {
        string t;
        int n;
        int c = 0;
        while (true) {
            if (s == "6174") {
                cout << c << endl;
                break;
            }
            t = s;
            sort(s.begin(), s.end());
            sort(t.begin(), t.end(), greater<char>());
            if (s == t) {
                cout << "NA" << endl;
                break;
            }
            n = to_i(t) - to_i(s);
            s = to_s(n);
            c++;
        }
    }
    return 0;
}

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

using namespace std;

int main(){
  string s;
  int l[4], r[4], m[4];
  while(1){
    cin >> s;
    if(s=="0000") break;
    if(s[0]==s[1] && s[0]==s[2] && s[0]==s[3]){
      cout << "NA" << endl; continue;
    }
    m[0]=s[0]-'0', m[1]=s[1]-'0', m[2]=s[2]-'0', m[3]=s[3]-'0';
    for(int i=0; ; ++i){
      if(m[0]==6 && m[1]==1 && m[2]==7 && m[3]==4){
	cout << i << endl; break;
      }
      l[0]=m[0], l[1]=m[1], l[2]=m[2], l[3]=m[3];
      sort(l, l+4);
      r[0]=l[0], r[1]=l[1], r[2]=l[2], r[3]=l[3];
      reverse(r, r+4);
      int k = r[0]*1000 + r[1]*100 + r[2]*10 + r[3]
	- l[0]*1000 - l[1]*100 - l[2]*10 - l[3];
      m[0]=k/1000, k-=m[0]*1000;
      m[1]=k/100, k-=m[1]*100;
      m[2]=k/10, k-=m[2]*10;
      m[3]=k;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <sstream>
#include <numeric>
#include <cstdio>
#include <cassert>
using namespace std;

string to_str(int n)
{
	string res;
	res.push_back('0' + n % 10000 /1000);
	res.push_back('0' + n % 1000 / 100);
	res.push_back('0' + n % 100 / 10);
	res.push_back('0' + n % 10 / 1);
	return res;
}

bool same_all(string s) { return count(s.begin(), s.end(), s[0]) == s.size(); }
int to_int(string s) { return atoi(s.data()); }

int main()
{
	for(string line; cin >> line && (line != "0000"); ) {
	  int times = 0;

		if(same_all(line)) {
			cout << "NA" << endl;
			continue;
		}
		
		for(; line != "6174"; times++) {
			sort(line.begin(), line.end());
			int small = to_int(line);
		
			reverse(line.begin(), line.end());
			int big = to_int(line);

			int next = big - small;

			line = to_str(next);
		}

		cout << times << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
	
int main(){
	while(1){
		int N,cnt;
		cin >>N;

		if(N==0000)
			break;

		if(N%1111==0){
			cout << "NA" <<endl;
			continue;
		}

		while(N!=6174){
			int n[4]={N/1000,N%1000/100,N%100/10,N%10};
			sort(n,n+4);
			int L=n[3]*1000+n[2]*100+n[1]*10+n[0];
			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>
#include<string>
#include<algorithm>
using namespace std;int main(){int n;while(true){cin>>n;if(n==0)break;if(n%1111==0)cout<<"NA"<<endl;else{int ans=0,count,s[4];while(true){if(n==6174)break;int a=10;for(int i=0;i<4;i++){s[i]=n%a/(a/10);a*=10;}sort(s,s+4);count=0;a=1;for(int i=0;i<4;i++){count+=a*s[i]-a*s[3-i];a*=10;}n=count;ans++;}cout<<ans<<endl;}}return 0;}

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

    cin >> input;
    while(input != 0)
    {
        if(input % 1111 == 0){
	    cout << "NA" << endl;
	    cin >> input;
	    continue;
        }
	while(input != 6174){
            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;
	c = 0;
        cin >> input;
    }
    return 0;
}

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

const string ANS = "6174";
const int N = 4;

int main(int argc, char const* argv[])
{
	string buf;
	int c;
	int l[N], s[N], res[N];
	bool na;
	while(cin >> buf){
		c = 0;
		na = true;
		if(buf == "0000") break;
		for(int i(0);i < N - 1;++i){
			na = na && (buf[i] == buf[i + 1]);
		}
		if(na) {
			cout << "NA" << endl;
			continue;
		}
		while(buf != ANS){
			for(int i = 0;i < N;++i){
				l[i] = s[i] = (int)(buf[i] - '0');
			}
			sort(l, l + N, greater<int>());
			sort(s, s + N);
			for(int i = N - 1;0 <= i;--i){
				res[i] = l[i] - s[i];
				if(res[i] < 0){
					--l[i - 1];
					res[i] += 10;
				}
			}
			for(int i = 0;i < N;++i){
				buf[i] = (char)(res[i] + '0');
			}
			++c;
		}
		cout << c << endl;
		
	}
	return 0;
}