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 <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)

string N;

int solve(){
  string s = (N);
  int cou = 0;
  while(s != "6174"){
    sort(s.begin(),s.end());
    if(s.front() == s.back()) return -1;
    int S = atoi(s.c_str());
    reverse(s.begin(),s.end());
    int L = atoi(s.c_str());
    s = to_string(L - S);
    while(s.size() < 4) s.push_back('0');
    cou++;
  }

  return cou;
}
int main(){
  while(cin >> N,N != "0000"){
    int ans = solve();
    if(ans == -1){
      cout << "NA" << endl;
    }
    else{
      cout << ans << endl;
    }
  }
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;

signed main(){
    cin.tie(0); 
    ios::sync_with_stdio(false);
    int n;
    int value[4];
    int count;
    int comp;
    int temp;
    bool judge;
    int l,s;
    cin>>n;
    while( n != 0){
        judge = true;
        for(int i=1;i<=9;i++){
            comp = i;
            for(int j=1;j<4;j++){
                comp = comp*10;
                comp += i;
            }
            if(comp == n){
                judge = false;
                break;
            }
        }
        if(judge){
            count=0;
            while(n != 6174){
                temp = n;
                for(int i=0;i<4;i++){
                    value[i] = temp % 10;
                    temp = temp / 10;
                }
                sort(value,value+4,greater<int>());
                l = 0;
                for(int i=0;i<4;i++){
                    l *= 10;
                    l += value[i];
                }
                sort(value,value+4);
                s = 0;
                for(int i=0;i<4;i++){
                    s *= 10;
                    s += value[i];
                }
                n = l - s;
                count++;
            }
            cout<<count<<"\n";
        }else{
            cout<<"NA\n";
        }
        cin>>n;
    }
    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<functional>
using namespace std;
int main(void){
	int n,i,a,j;
	int de,syo,tem[4];
	while(1){
		scanf("%000d",&n);
		if(n==0000)
			break;
		int cnt=0,flg=0;
		while(n!=6174){
			for(i=0;i<4;i++){
				a=1;
				for(j=0;j<3-i;j++){
					a*=10;
				}
				tem[i]=n/a;
				n-=tem[i]*a;
			}
		/*	if(tem[0]==tem[1]==tem[2]==tem[3]){
				flg=1;
				printf("%d %d %d %d\n",tem[0],tem[1],tem[2],tem[3]);
				break;
			}
	*/		sort(tem,tem+4);
			a=1000;
			de=syo=0;
			for(i=0;i<4;i++){
				syo+=tem[i]*a;
				a/=10;
			}
			sort(tem,tem+4,greater<int>());
			a=1000;
			for(i=0;i<4;i++){
				de+=tem[i]*a;
				a/=10;
			}
			if(de==syo){
				flg=1;
				break;
			}
			cnt++;
		//	printf("a=%d %d\n",de,syo);
			n=de-syo;
		}
		if(flg==0){
			printf("%d\n",cnt);
		}
		else {
			printf("NA\n");
		}
	}
	return 0;
}

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

int main(){
    int n;
    
    while(cin >> n,n){
        int cnt = 0;
        if(n == 6174){
            cout << cnt << endl;
            continue;
        }
        while(true){
            cnt++;
            int d[4],m=10,NA=0;
            for(int i = 0 ; i < 4 ; i++){
                d[i] = n % m;
                n /= 10;
                if(i&&d[i]==d[i-1]) NA++;
            }
            if(NA == 3){
                cout << "NA" << endl;
                break;
            }
            sort(d,d+4);
            int L=d[3]*1000+d[2]*100+d[1]*10+d[0];
            int S=d[0]*1000+d[1]*100+d[2]*10+d[3];
            if(L-S==6174){
                cout << cnt << endl;
                break;
            }
            else n = L-S;
        }
    }
    
    return 0;
}

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

int main(){
	int n,nn;
	while(cin >> n,n){
		if(n % 1111 == 0) {
			cout << "NA" << endl;
		}else{
			int i = 0;
			for(;n != 6174;i++){
				int num[4] = {n / 1000,n / 100 % 10,n / 10 % 10,n % 10};
				sort(num,num + 4,greater<int>());
				n = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
				sort(num,num + 4);
				nn = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
				n = n - nn;
			}
			cout << i << endl;
		}
	}
	return 0;
}

C++: #include<stdio.h>

int main(){
  int i,n,l[4],s[4];

  scanf("%d",&n);

  while(n != 0){
    int cnt=0;
    int a=n;

    for(i=3;i>=0;i--){
      int aa=a%10;
      l[i]=aa;
      a /= 10;
    }

    if(n == 6174){
      printf("0\n");
    }else if(l[0] == l[1] && l[0] == l[2] && l[0] == l[3]){
      printf("NA\n");
    }else{
     
      while(n != 6174){
	int len1,len2;
	for(len1=0;len1<3;len1++){
	  for(len2=0;len2<3-len1;len2++){
	    if(l[len2] < l[len2+1]){
	      int temp = l[len2];
	      l[len2] = l[len2+1];
	      l[len2+1] = temp;
	    }
	  }
	}   
	
	int j=0;
	for(i=3;i>=0;i--){
	  s[j]=l[i];
	  j++;
	}
	int ll=0,ss=0,ba=1000;
	for(i=0;i<4;i++){
	  ll += l[i]*ba;
	  ss += s[i]*ba;
	  ba /= 10;
	}
	n = ll - ss;
	cnt++;
	a=n;
	for(i=3;i>=0;i--){
	  int aa=a%10;
	  l[i]=aa;
	  a /= 10;
	}
      }
      printf("%d\n",cnt);
    }
    scanf("%d",&n);
  }
  return 0;
}

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

int to_i(const string &s) {
    int n;
    istringstream is(s);
    is >> n;
    return n;
}

string to_s(int n) {
    char buf[256];
    sprintf(buf, "%04d", n);
    return string(buf);
}

int main() {
    string s;
    while (cin >> s, s != "0000") {
        string t;
        int n;
        int c = 0;
        while (true) {
            if (s == "6174") {
                cout << c << endl;
                break;
            }
            t = s;
            sort(s.begin(), s.end());
            sort(t.begin(), t.end(), greater<char>());
            if (s == t) {
                cout << "NA" << endl;
                break;
            }
            n = to_i(t) - to_i(s);
            s = to_s(n);
            c++;
        }
    }
    return 0;
}

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

using namespace std;

int main(){
  string s;
  int l[4], r[4], m[4];
  while(1){
    cin >> s;
    if(s=="0000") break;
    if(s[0]==s[1] && s[0]==s[2] && s[0]==s[3]){
      cout << "NA" << endl; continue;
    }
    m[0]=s[0]-'0', m[1]=s[1]-'0', m[2]=s[2]-'0', m[3]=s[3]-'0';
    for(int i=0; ; ++i){
      if(m[0]==6 && m[1]==1 && m[2]==7 && m[3]==4){
	cout << i << endl; break;
      }
      l[0]=m[0], l[1]=m[1], l[2]=m[2], l[3]=m[3];
      sort(l, l+4);
      r[0]=l[0], r[1]=l[1], r[2]=l[2], r[3]=l[3];
      reverse(r, r+4);
      int k = r[0]*1000 + r[1]*100 + r[2]*10 + r[3]
	- l[0]*1000 - l[1]*100 - l[2]*10 - l[3];
      m[0]=k/1000, k-=m[0]*1000;
      m[1]=k/100, k-=m[1]*100;
      m[2]=k/10, k-=m[2]*10;
      m[3]=k;
    }
  }
  return 0;
}