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<algorithm>
#include<iostream>
#include<string>

int main()
{
	std::string N;
	while( std::cin >> N, N != "0000" )
	{
		int cnt = 0;

		while( N != "6174" )
		{
			std::string L = N, S = N;

			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			for( int i = 0; i != N.size(); ++i )
			{
				N[i] = L[i] - S[i] + '0';
				if( i > 0 && N[i] < '0' )
					--N[i-1], N[i] += 10; 
			}

			++cnt;

			if( N == "0000" )
			{
				std::cout << "NA" << std::endl;
				break;
			}
		}

		if( N != "0000" )
			std::cout << cnt << std::endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>

#define F first
#define S second

using namespace std;

typedef pair<int,int>P;
typedef long long ll;

int main(){
  int n;
  int l,s;
  int buff[4];
  int temp;
  int cnt;
  
  while(1){
    scanf("%d",&n);
    if(n==0) break;

    if(n%1111==0){
      printf("NA\n");
    }else{
      
      for(int i=3;i>=0;i--){
	buff[i]=n%10;
	n/=10;
      }
      
      cnt=0;
      while(!(buff[0]==6&&buff[1]==1&&buff[2]==7&&buff[3]==4)){
	s=l=0;
	sort(buff,buff+4);
	for(int i=0;i<4;i++){
	  s*=10;
	  s+=buff[i];
	}
	
	for(int i=3;i>=0;i--){
	  l*=10;
	  l+=buff[i];
	}
	
	temp=l-s;
	for(int i=3;i>=0;i--){
	  buff[i]=temp%10;
	  temp/=10;
	}
	cnt++;
      }
      
      printf("%d\n",cnt);
    }
  }

  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 1000


void calc(int n){

  if(n%1111 == 0){
    puts("NA");
    return;
  }

  char l[5], s[5];

  for(int i=0;;i++){
    if(n == 6174){
      printf("%d\n",i);
      break;
    }
    
    sprintf(l,"%04d",n);
    sprintf(s,"%04d",n);
    
    sort(l,l+4);
    sort(s,s+4);
    reverse(l,l+4);
    
    n = atoi(l) - atoi(s);
  }

}
  

int main(){

  int n;

  while(scanf("%d",&n)!=EOF){
    if(n==0) break;
    calc(n);
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int solve(string &s, int cnt = 0){
  while(s != "6174"){
    if(s == "0000")return -1;
    cnt++;
    sort(s.begin(), s.end());
    string tmp = s;
    reverse(tmp.begin(), tmp.end());
    int a = stoi(s), b = stoi(tmp);
    s = to_string(b - a);
    while(s.length() != 4)s.push_back('0');
  }
  return cnt;
}

int main(int argc, char *argv[]){
  string s;
  while(cin >> s){
    if(s == "0000")break;
    int ans = solve(s);
    if(ans != -1)
      std::cout << ans << std::endl;
    else
      std::cout << "NA" << std::endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    while(cin>>s){
        if(s=="0000")break;
        int ans=0;
        if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
            ans=-1;
            goto FIN;
        }
        while(s!="6174"){
            string sa,sb;
            char ca[4]={s[0],s[1],s[2],s[3]};
            sort(ca,ca+4);
            for(int i=0;i<4;i++){
            sa.push_back(s[i]);   
            sb.push_back(s[3-i]);
            }
           
            int af=0,bf=0;
            for(int i=0;i<4;i++){
                if(sa[i]==0&&af==0)sa.erase(0,1);
                else af++;
               
                if(sb[i]==0&&bf==0)sb.erase(0.1);
                else bf++;
                }
            int a=stoi(sa);
            int b=stoi(sb);
            int c=abs(a-b);
           
            string sc=to_string(c);
            int z=sc.size();
            for(int i=0;i<4-z;i++)sc.insert(0,"0");
            s=sc;
            ans++;
            }
            FIN:
            if(ans==-1)cout<<"NA"<<endl;
            else cout<<ans<<endl;
        }
        return 0;
}

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(n == 0)break;
		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<iostream>
#include<algorithm>

using namespace std;
int main()
{
	int su, n[5];
	int i;
	int d, s;
	int ke,cnt;
	int f;
	while (cin >> su, su != 0)
	{
		cnt = 0;
		f = 1;
		while (su != 6174)
		{
			//cout << cnt << endl;
			cnt++;
			ke = 1000;
			for (i = 0; i < 4; i++)
			{
				n[i] = su / ke;
				su -= su / ke * ke;
				ke = ke / 10;
			}
			if (n[0] == n[1] && n[2] == n[3] && n[1] == n[2])
			{
				cout << "NA" << endl;
				f = 0;
				break;
			}
			sort(n, n + 4);
			d = s = 0;
			ke = 1000;
			for (i = 0; i < 4; i++)
			{
				s += n[i] * ke;
				ke = ke / 10;
			}
			ke = 1000;
			for (i = 3; i >= 0; i--)
			{
				d += n[i] * ke;
				ke = ke / 10;
			}
			su = d - s;
		}
		if(f) cout << cnt << endl;
	}
	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,r[4],l,s,x,y,z,a,i,j,flg;
	while(1){
		scanf("%d",&n);
		if(n==0000){
			break;
		}
		flg=1;
		a=0;
		while(n!=6174){
			y=0;
			x=1000;
			for(i=0;i<4;i++){
				r[i]=n/x;
				n=n%x;
				x=x/10;
				if(r[0]==r[i]) y++;
			}
			if(y==4) {
				flg=0;
				break;
			}
			l=0; s=0;
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(r[i]>r[j]){
						z=r[i];
						r[i]=r[j];
						r[j]=z;
					}
				}
			}
			x=1000;
			for(i=0;i<4;i++){
				s=r[i]*x+s;
				x=x/10;
			}
			printf("x=%d ",x);
			x=1;
			for(i=0;i<4;i++){
				l=r[i]*x+l;
				x=x*10;
			}
			n=l-s;
			a++;
		}
		if(flg==1) printf("%d\n",a);
		if(flg==0) printf("NA\n");
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;

int main()
{
	while (1) {
		string s;
		cin >> s;
		if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0')
			break;
		if (s == "6174") {
			cout << 0 << endl;
		}
		else if (s[0] == s[1] &&  s[2] == s[3] && s[1] == s[2])
			cout << "NA" << endl;
		else {
			ll ans = 0;
			while (s != "6174") {
				while (s.size() != 4)
					s = '0' + s;
				
				int a[4];
				for (int i = 0; i < 4; i++)
					a[i] = s[i] - '0';

				sort(a, a + 4);

				ll small=0,large=0;
				for (int i = 0; i < 4; i++)
					large += a[i] * (ll)pow(10, i);
				
				reverse(a,a+4);
				for (int i = 0; i < 4; i++) 
					small += a[i] * (ll)pow(10, i);
				
				s = to_string(large - small);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}