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 <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;

	cin >> n;

	while (n > 0) {
		int a[4];
		a[0] = n / 1000;
		a[1] = (n % 1000) / 100;
		a[2] = (n % 100) / 10;
		a[3] = n % 10;

		if (a[0] == a[1] && a[0] == a[2] && a[0] == a[3])
			cout << "NA" << endl;
		else {
			int t = 0;

			while (n != 6174) {
				a[0] = n / 1000;
				a[1] = (n % 1000) / 100;
				a[2] = (n % 100) / 10;
				a[3] = n % 10;
				sort(a, a + 4);
				n = (a[3] - a[0]) * 999 + (a[2] - a[1]) * 90;
				t++;
			}

			cout << t << endl;
		}

		cin >> n;
	}

	return 0;
}

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

	using namespace std;

	int main()
	{
		int m, n[4], mi[4], ma[4], na;
		int	max_int, min_int, n_int, ans;
		bool same = false;
			while (scanf("%1d", &n[i])){
				na = n[0];
				for (int i = 1; i < 4; i++){
					if (na != n[i]) same = true;
				}
				if (!same) cout << "NA" << endl;

			while(n[0] != 6 || n[1] != 1 || n[2] != 7 || n[3] != 4){
				if (!same) break;
				sort(n, n + 4);
				
				for (int i = 0; i < 4; i++){
					mi[i] = n[i];
					ma[3 - i] = n[i];
				}

				for (int i = 0; i < 4; i++){
					max_int = ma[i] * 10 ^ i;
					min_int = mi[i] * 10 ^ i;
				}
				n_int = max_int - min_int;
				for (int i = 0; i < 4; i++){
					n[i] = n_int / (10 ^ (3 - i)) % 10;
				}
				ans++;
			}

			if (!same) cout << ans << endl;
		}
		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++: import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (;;) {
            int n=sc.nextInt();
            if(n==0) {
                break;
            }
            Integer[] l=d(n);
            if(l[0]==l[1]&&l[1]==l[2]&&l[2]==l[3]) {
                System.out.println("NA");
                continue;
            }
            int c=0;
            while(n!=6174) {
                c++;
                l=d(n);
                Arrays.sort(l,Comparator.reverseOrder());
                int a=c(l);
                Arrays.sort(l);
                int b=c(l);
                n=a-b;
            }
            System.out.println(c);
        }
    }
    static int c(Integer[] l) {
        int n=0;
        for(int i=0,j=1000;i<4;i++,j/=10) {
            n+=l[i]*j;
        }
        return n;
    }
    static Integer[] d(int n) {
        Integer[] l=new Integer[4];
        for(int i=0,j=1000;i<4;i++,j/=10) {
            l[i]=n/j;
            n%=j;
        }
        return l;
    }
}

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

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)
using namespace std;

int to[10000];
int memo[10000];

void init(){
	string str, rstr;
	int L,S;
	stringstream ss;
	rep(i,10000)memo[i] = -1;
	rep(i,10000){
		ss.clear();
		ss<<i;
		ss>>rstr;
		sort(rstr.begin(),rstr.end());
		str = rstr;
		reverse(str.begin(), str.end());
		ss.clear();
		ss << (str + " " + rstr);
		ss >> L >> S;
		to[i] = L-S;
	}
}

int main(void){
	int n;
	init();
	
	while(scanf("%d", &n),n){
		int ans = 0;
		int n_ = n;
		if(to[n] == 0){
			puts("NA");
			continue;
		}
		if(memo[n] != -1){
			ans = memo[n];
		}else{
			while(n != 6174){
				n = to[n];
				ans++;
			}
			memo[n_] = ans;
		}
		printf("%d\n", ans);
	}
	
	return 0;
}

C++: #include<stdio.h>

int main(){

char c1,c2,c3,c4,ch5;
int n1=0,n2=0,n3=0,n4=0,N=0,S=0,P=0,K=0;
int n[5]={};

while(scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&ch5)!=EOF){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
  for(int i=0;i<4;i++)
	n[i]=0;
int cnt=0;


n[0]=c1-'0';n[1]=c2-'0';n[2]=c3-'0';n[3]=c4-'0';


for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}

N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

P=N-S;
if(P!=6174)cnt++;

while(P!=6174){
N=P;
K=P;n[0]=N/1000;
K=N%1000;n[1]=K/100;
K=N%100;n[2]=K/10;
K=N%10;n[3]=K;

//printf(" %d\n",N);

for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}


N=1000*n[0]+100*n[1]+10*n[2]+n[3];
S=1000*n[3]+100*n[2]+10*n[1]+n[0];

cnt++;
P=N-S;

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

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

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s){
  istringstream iss(s);
  int ret;
  iss >> ret;
  return ret;
}


int main(){
  string s;
  while(cin >> s){
    if(s == "0000") break;
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
      cout << "NA" << endl;
      continue;
    }
    int cnt = 0;
    
    while(s != "6174"){
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a-b);
    }
    cout << cnt << endl;
  }
  return 0;
}

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

int main(void){
	int a,b,c,d;
	while(scanf("%1d%1d%1d%1d",&a,&b,&c,&d)!=EOF,!(a==0&&b==0&&c==0&&d==0)){
		if(a==b&&b==c&&c==d&&d==a){
			cout<<"NA"<<endl;
		}else{
			int count=0;
			while(!(a==6&&b==1&&c==7&&d==4)){
				vector<int> nums,numl;
				nums.push_back(a);
				nums.push_back(b);
				nums.push_back(c);
				nums.push_back(d);
				numl = nums;
				sort(nums.begin(),nums.end());
				sort(numl.begin(),numl.end(),greater<int>());
				int s = nums[0]*1000+nums[1]*100+nums[2]*10+nums[3];
				int l = numl[0]*1000+numl[1]*100+numl[2]*10+numl[3];
				int N = l-s;
				a=N/1000;
				b=(N%1000)/100;
				c=(N%100)/10;
				d=N%10;
				++count;
			}
			cout<<count<<endl;
		}
	}
	return 0;
}