Go: package main

import (
	"fmt"
	"sort"
)

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]
	return solve(l-s) + 1
}

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<vector>
#include<algorithm>
#include<sstream>
#include<string>
using namespace std;
int cnv(string str,bool gFlag){
    sort(str.begin(),str.end());
    if(gFlag)reverse(str.begin(),str.end());
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
int main(){
    int n;
    while(cin>>n,n){
        int cnt=0,left=0;
        bool flag=false;
        while(n!=6174){
            stringstream ss;
            ss<<n;
            string str;
            ss>>str;
            n=cnv(str,true)-cnv(str,false);
            cnt++;
            if(left==n){
                flag=true;
                break;
            }
            left=n;
        }
        if(flag)cout<<"NA"<<endl;
        else cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	char str[5];
	sprintf( str, "%04d", n );

	return str;
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		bool fl = true;

		rep( i, 3 )
			fl &= L[i+1] == L[i];
		if( fl )
		{
			std::cout << "NA" << std::endl;
			continue;
		}

		int ans = 0;
		while( L != "6174" )
		{
			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		std::cout << ans << std::endl;
	}

	return 0;
}

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