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 <stdio.h> // C++???????????????
#include <string.h> // c++???????????????
#include <math.h> // c++???????????????
#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long int lli;
#define PI 3.1415926535897932
#define MAX(a,b) ((a>b)?(a):(b)) // ?????§????????????
#define MIN(a,b) ((a<b)?(a):(b)) // ????°?????????????
#define INRANGE(a,b,c) ((a<=b&&b<=c)?(1):(0)) // a<=b<=c??????????????????????????????
#define N 4

void swap(int &a,int &b){ // c??????*
	int w=a;
	a=b;
	b=w;
}

int getN(int num,int digit){ // ?????????????????°????????????
	int i=1,j;
	for(j=1;j<digit;i*=10,j++);
	return (num/i)%10;
}

int zeller(int y,int m,int d){ // ?????§??????
	if(m<3){y--; m+=12;}
	return (y+y/4-y/100+y/400+(13*m+8)/5+d)%7;
	// 0=sunday!
}

int gcd(int a,int b){	// ?????§??¬?´???°
	if(b==0) return a;
	return gcd(b,a%b);
}

int lcm(int a,int b){	// ????°???¬?????°
	return a*b/gcd(a,b);
}

int stoi(const string str){ // ?????????????????´??°
	int i=0,j;
	int len=str.length();
	int w=0;
	for(i=1,j=1;i<=len && INRANGE('0',str[len-i],'9');i++,j*=10){
		w+=(str[len-i]-'0')*j;
	}
	if(str[0]=='-')w*=-1; // ??????????????????
	return w;
}

string itos(int a){ // ??´??°???????????????
	int i;
	string str="";
	int keta;
	if(a<0){str+='-';a*=-1;} // ?????°??????????????????
	for(keta=10;a/keta!=0;keta*=10);
	for(i=keta/10;i!=0;i/=10){
		str+=((a/i)%10)+'0';
	}
	return str;
}

int main(void){
	string str;
	string str1,str2;
	int a,b; //a>b
	int c;
	while(cin>>str,str!="0000"){
		c=0;
		while(str!="6174"){
			for(int i=0;i<N;i++){
				for(int j=i+1;j<N;j++){
					if(str[i]<str[j])swap(str[i],str[j]);
				}
			}
			str1=str;
			for(int i=0;i<N/2;i++) swap(str[i],str[N-i-1]);
			str2=str;
			str=itos(stoi(str1)-stoi(str2));
			if(str=="0"){cout<<"NA"<<endl; goto LOOP;}
			c++;
		}
		cout<<c<<endl;
LOOP:;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int solve(int num,int& cnt) {
	if (num == 6174)return cnt;
	cnt++;
	int s[5];
	for (int i = 0; i < 4; i++) {
		s[i] = num % 10;
		num -= s[i];
		num /= 10;
	}
	sort(s, s + 4);
	return solve((s[3] - s[0]) * 999 + (s[2] - s[1]) * 90, cnt);
}
int main() {
	int n;
	while (cin >> n) {
		if (!n)return 0;
		else if (n % 1111 == 0) {
			cout << "NA\n";
			continue;
		}
		int cnt;
		solve(n, cnt);
		cout << cnt << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

pair<int, int> to_L_S(string s) {
    vector<int> a;
    for (int i = 0; i < s.length(); i++) {
        a.push_back((int)(s[i] - '0'));
    }
    while (a.size() <= 3) {
        a.push_back(0);
    }
    sort(a.begin(), a.end());
    int L = 0;
    int S = 0;
    int k[4] = {1, 10, 100, 1000};
    for (int i = 0; i < 4; i++) {
        L += (a[i] * k[i]);
        S += (a[i] * k[3 - i]);
    }
    return make_pair(L, S);
}

int main() {

    for (int i = 0; i < 1000000; i++) {
        string s;
        cin >> s;
        if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
            if (s[0] == '0') {
                return 0;
            }
            cout << "NA\n";
        } else if (s == "6174") {
            cout << "0\n";
        } else {
            pair<int, int> p = to_L_S(s);
            int cnt = 1;
            while (p.first - p.second != 6174) {
                cnt++;
                int N = p.first - p.second;
                // cout << p.first << " " << p.second << "\n";
                // cout << N << "\n";
                p = to_L_S(to_string(N));
            }
            cout << cnt << "\n";
        }

    }
    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;
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      n=l-s;
      assert(ans<100);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;

bool input(int a[]){
	int i, inp;
	scanf("%d",&inp);
	if(inp == 0)
		return false;
	for(i = 3;i >= 0;i--){
		a[i] = inp % 10;
		inp /= 10;
	}
	return true;
}
int main(void){
	int a[4], b[4], i, count;
	const int suc[4] = {6,1,7,4};
	while(input(a)){
		count = 0;
		while(1){
			for(i = 0;i < 4;i++)
				if(suc[i] != a[i]) break;
			if(i == 4){
				printf("%d\n",count);
				break;
			}
			for(i = 1;i < 4;i++)
				if(a[0] != a[i]) break;
			if(i == 4){
				printf("NA\n");
				break;
			}
			for(i = 0;i < 4;i++)
				b[i] = a[i];
			sort(a,a + 4,greater<int>());
			sort(b,b + 4);
			for(i = 0;i < 4;i++)
				printf("%d",b[i]);
			printf("\n");
			for(i = 3;i >= 0;i--){
				a[i] -= b[i];
				if(a[i] < 0){
					a[i - 1]--;
					a[i] += 10;
				}
			}
			count++;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(){
  string n;
  while(1){
    cin >> n;
    if(n == "0000") break;
    int num[4];
    for(int i = 0;i < 4;i++) num[i] = n[i] - '0';

    if(n == "6174"){
      cout << 0 << endl;
    }else if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
      cout << "NA" << endl;
    }else{


      int cnt = 1;
      int tmpa,tmpb,tmpc;
      while(1){
        tmpa = tmpb = 0;
        sort(num,num+4);
        int tmp = 1;
        for(int i = 0;i < 4;i++){
          tmpa += num[i] * tmp;
          tmp *= 10;
        }
        tmp = 1;
        for(int i = 0;i < 4;i++){
          tmpb += num[3-i] * tmp;
          tmp *= 10;
        }
        tmpc = tmpa - tmpb;
        if(cnt == 10) break;
        if(tmpc == 6174){
          cout << cnt << endl;
          break;
        }
        for(int i = 0;i < 4;i++){
          num[i] = tmpc % 10 ;
          tmpc /= 10;
        }
        cnt++;
      }
    }
  }

  return 0;
}