Go: package main

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

var memo []int

func solve(v int) int {
	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([]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 <string>
#include <algorithm>
#include <sstream>

using namespace std;

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(void)
{
	int a;
	while (cin >> a, a){
		if (a == 0) break;
		string n = toString(a);
		if (n.length() < 4) {
			for (int i = 0; i <= 4-n.length(); i++) {
				n += '0';
			}
		}
		if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		string N = n;
		while (1){
			if (N == "6174"){
				cout << cnt << endl;
				break;
			}
			// cout << N << endl;
			// cin >> tmp;
			string L = N; sort(L.rbegin(), L.rend());
			string S = N; sort(S.begin(), S.end());
			// cout << L << " " << S << endl;
			int d = toInt(L) - toInt(S);
			N = toString(d);
			if (N.length() < 4) {
				for (int i = 0; i <= 4-N.length(); i++) {
					N += '0';
				}
			}
			cnt++;
		}
	}

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

C++: #include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
int dp[10002];
int calcL(string s){
  sort(s.begin(), s.end(),greater<int>());
  //cout << __FUNCTION__ << ":" << s << endl;
  return atoi(s.c_str());
}
int calcS(string s){
  sort(s.begin(), s.end());
  //cout << __FUNCTION__ << ":" << s << endl;
  return atoi(s.c_str());
}
string toString(int n){stringstream ss; ss << n; return ss.str();}
int rec(string s){
  int idx = atoi(s.c_str());
  if(dp[idx] != -1){
    return dp[idx];
  }
  if(s == "6174"){
    return 0;
  }
  if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
    return -1;
  }
  int diff = calcL(s) - calcS(s);
  
  s = toString(diff);
  if(s.size() < 4){
    string _s = "";
    for(int i = s.size(); i < 4; i++){
      _s += "0";
    }
    _s += s;
    s = _s;
  }
  int res = rec(s);
  return (dp[diff] = (res < 0?-2:res + 1));
}
void solve(string s){
  fill(dp, dp+ 10002, -1);
  dp[6174] = 0;
  int cnt = 0;
  int input = atoi(s.c_str());
  dp[input] = rec(s);
  if(dp[input] == -1)cout << "NA" << endl;
  else cout << dp[input] << endl;
}
int main(){
  string s;
  while(cin >> s){

    if(s == "0000")break;
    solve(s);
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int N;
	while(1){
		cin>>N;
		if(N==0000)break;
		if(N%1111==0){
			cout<<"NA"<<endl;
		}
		   int cnt = 0;
 
        while(N!=6174){
            int n[4];
            n[0] = N/1000;
            n[1] = N/100%10;
            n[2] = N/10%10;
            n[3] = 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<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	int n;
	int str[4];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == 0)
				break;
			else
				std::cout << "NA" << std::endl;
		}
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3]) {
				std::cout << "NA" << std::endl;
				goto a;
			}
			++cnt;
			std::sort(str, str + 4, std::greater<int>());
			int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			std::sort(str, str + 4);
			int	s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			n = l - s;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	a:;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#include<string.h>
using namespace std;
signed main(){
  int n;
 
  while(cin>>n,n){
    int sum=0;
    int l[10]={};
    int s[10]={};
    
  while(true){
      if(n==6174){
	cout<<sum<<'\n';
	break;
      }
      for(int i=3;i>=0;i--){
	l[i]=n%10;
	s[i]=n%10;
	n/=10;
      }
      sort(s,s+4);
      sort(l,l+4,greater<int>());
      if(l[0]==s[0] && l[1]==s[1] && l[2]==s[2] && l[3]==s[3] && sum==0){
	cout<<"NA"<<'\n';
	break;
      }
      sum++;
      n=(l[0]*1000+l[1]*100+l[2]*10+l[3])-(s[0]*1000+s[1]*100+s[2]*10+s[3]);
    }
  }
  return(0);
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n)n){
        int cnt = 0;
        bool flg = 0;
        while(1){
            if(n == 6174)break;
            if(n%1111 == 0){
                flg = 1;
                break;
            }
            int a[4]={0};
            a[0] = n/1000;
            a[1] = (n%1000)/100;
            a[2] = (n%100)/10;
            a[3] = n%10;
            int L[4],S[4];
            sort(a,a+4);
            for(int i = 0; i < 4; i++){
                L[3-i] = S[i] = a[i];
            }
            int l=0,s=0;
            for(int i = 0; i < 4; i++){
                l += L[3-i] * pow(10,i);
                s += S[3-i] * pow(10,i);
            }
            n = l - s;
            cnt++;
        }
        if(flg)puts("NA");
        else printf("%d\n",cnt);
    }
	return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
     int n,d[4],max,min,w[4]={1000,100,10,1};
     int c=0,i,j,tmp;
     cin >>n;
     while(n!=6174)
     {
       for(i=0;i<4;i++)
       {
             d[i]=n/w[i];
             n=n%w[i];
       }
       for(i=0;i<3;i++)
       {
          for(j=i+1;j<4;j++)
           {
                if(d[i]<d[j])
                 {
                    tmp=d[i];
                    d[i]=d[j];
                    d[j]=tmp;
                  }
            }
       }
        for(i=0;i<4;i++)
         {
              max=max+(d[i]*w[i]);
              min=min+(d[i]*w[3-i]);
         }
         n = max-min;
         c++;
     }
     cout << n << endl;
    return 0;
}