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

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

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

void ssort(string& s,bool _greater){
    for(int i=0;i<4;i++){
        for(int j=i+1;j<4;j++){
            if(_greater){
                if(s[i]>s[j]){
                    swap(s[i],s[j]);
                }
            }
            else{
                if(s[i]<s[j]){
                    swap(s[i],s[j]);
                }
            }
        }
    }
}

int main() {

    string s;

    while (1) {

        cin >> s;

        if(s[0]==s[1]&&s[1]&&s[2]&&s[2]==s[3]&&s[3]==s[0]){
            if(s[0]=='0'){
                break;
            }
            cout << "NA" << endl;
            continue;
        }

        int ans = 0;

        while(1) {


            if (s == "6174") {
                cout << ans << endl;
                break;
            }

            ans++;

            string s1, s2;

            s1 = s2 = s;

            //sort(s1.begin(),s1.end());
            //sort(s2.begin(),s2.end(),greater<string>());

            ssort(s1, false);
            ssort(s2, true);

            int a1, a2;

            stringstream ss;

            ss << s1;
            ss >> a1;
            ss.clear();
            ss << s2;
            ss >> a2;
            a1 -= a2;

            ss.clear();

            ss << a1;

            ss >> s;

            if(s.size() != 4){
                while(s.size()!=4){
                    s = '0' + s;
                }
            }
        }

    }

    return 0;
}