Go: package main

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

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		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(map[int]int)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include <stdio.h>
#include <stdlib.h>
const int TARGET = 6174;
const int LEN = 4;

char num[LEN];

void sort(int reverse){
  int i, j, tmp;
  for(i = 0; i < LEN; ++i)
    for(j = i + 1; j < LEN; ++j){
      if((reverse == 0 && num[i] > num[j])
          || (reverse == 1 && num[i] < num[j])){
        tmp = num[i];
        num[i] = num[j];
        num[j] = tmp;
      }
    }
}

void convert(int n){
  int i;
  for(i = LEN - 1; i >= 0; --i){
    num[i] = (n % 10) + '0';
    n /= 10;
  }
}

int isSame(){
  int i, j;
  for(i = 0; i < LEN; ++i)
    for(j = i + 1; j < LEN; ++j)
      if(num[i] != num[j])
        return 0;
  return 1;
}

//int main(void) {
//  int n, i, cnt = 0;
//  scanf("%d", &n);
//  convert(n);
//  if(isSame()){
//    printf("NA\n");
//    return 0;
//  }
//
//  while(atoi(num) != TARGET){
//    sort(0);
//    int s = atoi(num);
//    sort(1);
//    int b = atoi(num);
//    convert(b - s);
//    ++cnt;
//  }
//
//  printf("%d\n", cnt);
//  return 0;
//}
//
int main(void) {
  int n, i, cnt = 0;
  while(1){
    scanf("%d", &n);
    if(!n) break;
    convert(n);
    if(isSame()){
      printf("NA\n");
      return 0;
    }

    while(atoi(num) != TARGET){
      sort(0);
      int s = atoi(num);
      sort(1);
      int b = atoi(num);
      convert(b - s);
      ++cnt;
    }

    printf("%d\n", cnt);
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  while(1){
  int count=0;
  int N;
  cin>>N;
  if(N==0)
  break;
  if(N%1111==0){
    cout<<"NA"<<endl;
    continue;
  }
   int L,S;
  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);
  L=1000*n[3]+100*n[2]+10*n[1]+n[0];
  S=1000*n[0]+100*n[1]+10*n[2]+n[3];
  N=L-S;
  count++;
 }
 cout<<count<<endl;
 }
 return 0;
}

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