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

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>
#include<iomanip>
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(){
    string str;
    while(cin>>str,str!="0000"){
        bool flag=false;
        for(int i=1;i<4;i++)
            if(str[0]!=str[i])flag=true;

        if(!flag){
            cout<<"NA"<<endl;
            continue;
        }
        int cnt=0;
        while(str!="6174"){
            int n=cnv(str,true)-cnv(str,false);
            stringstream ss;
            ss<<n;
            ss>>str;
            cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;




int main(){

	while (true){
		int n, tmp1 = 0, tmp2 = 0, a[4], cnt = 0;
		bool j = true;
		cin >> n;
		if (n == 0)break;
		tmp1 = n;
		while (tmp1 != 6174){
			rep(i, 4){
				a[i] = tmp1 % 10;
				tmp1 /= 10;
			}
			if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3]){
				j = false;
				break;
			}
			sort(a, a + 4);
			tmp1 = a[0] + a[1] * 10 + a[2] * 100 + a[3] * 1000;
			tmp2 = a[3] + a[2] * 10 + a[1] * 100 + a[0] * 1000;
			tmp1 -= tmp2;
			cnt++;
		}
		if (j)cout << cnt << endl;
		else cout << "NA" << endl;
	}

	return 0;
}

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

/* {{{ */
#define PI(N) ((M_PI * (N)) / 180) //?§???????????????¢??????????????????
#define CL(N) (cout << (N) << endl)
#define DCT(N) (cout << "debug " << (N) << endl)
#define REP(i, N) for (int i = 0; i < (N); i++)
/* }}} */

int main()
{
    int n[4];
    string str;
 
    while (cin >> str, str != "0000"){
        REP(i, 4){
            n[i] = str[i] - '0';
        }
 
        if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
            CL("NA");
        }
        else {
            int c = 0;
            while (n[0] != 6 || n[1] != 1 || n[2] != 7 || n[3] != 4){
                 
                char l[4], s[4];
                REP(i, 4){
                    l[i] = s[i] = n[i];
                }
 
                sort(l, l + 4);
                reverse(l, l + 4);
                sort(s, s + 4);
 
                for (int i = 3; 0 <= i; i--){
                    n[i] = l[i] - s[i];
                    if (n[i] < 0){
                        n[i] += 10;
                        l[i - 1]--;
                    }
                }
                c++;
            }
            CL(c);
        }
    }
    return (0);
}

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

int main(){
  char s[5];
  while(scanf("%s",s),strcmp(s,"0000")){
    bool f = true;
    for(int i=0;i<3;i++)f &= (s[i]==s[i+1]);
    if(f){ printf("NA\n"); continue; }

    int cnt = 0;
    while(strcmp(s,"6174")){
      sort(s,s+4);
      int S = atoi(s);
      reverse(s,s+4);
      int L = atoi(s);
      char N[5]; sprintf(N,"%04d",L-S);
      for(int i=0;i<4;i++)s[i] = N[i];
      cnt++;
    }
    printf("%d\n",cnt);
  }
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

class cl
{
public:
	int s[4];

	void sortw(int a)
	{
		for(int i = 0; i < 4; i++)
		{
			s[i] = a % 10;
			a /= 10;
		}
		sort(s,s + 4);
	}

	int minw()
	{
		int w = 0;
		for(int i = 0; i < 4; i++)
		{
			w *= 10;
			w += s[i];
		}
		return w;
	}

	int maxw()
	{
		int w = 0;
		for(int i = 0; i < 4; i++)
		{
			w *= 10;
			w += s[3 - i];
		}
		return w;
	}
};

int func(int a)
{
	cl w;
	w.sortw(a);
	return w.maxw() - w.minw();
}

int stoi(char* a)
{
	int w = 0;
	for(int i = 0; i < 4; i++)
	{
		w *= 10;
		w += a[i] - '0';
	}
	if(w % 1111 == 0 && w != 0)
	{
		return -1;
	}
	return w;
}

int kai(int a)
{
	for(int i = 0; i < 10000; i++)
	{
		if(a == 6174)
		{
			return i;
		}
		a = func(a);
	}
}

int main()
{
	while(1)
	{
		char w[10];
		scanf("%s",w);
		int a = stoi(w);
		if(a == 0)
		{
			break;
		}
		if(a < 0)
		{
			printf("NA\n");
		}
		else
		{
			printf("%d\n",kai(a));
		}
	}
	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){
    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<stdio.h>
int main(void)
{
	int n,i,w,p[10],h[10],flg,c,g[10],j;
	while(1){
		flg=0;
		c=0;
	scanf("%d",&n);
	if(n==0000)	break;
	while(1){
		if(n==6174) break;
		if(n<1000){
			h[0]=0;
		}
		else{
			h[0]=n/1000;
		}
		if(n<100){
			h[1]=0;
		}
		else{
			h[1]=(n%1000)/100;
		}
		if(n<10){
			h[2]=0;
		}
		else{
			h[2]=((n%1000)%100)/10;
		}
		h[3]=(((n%1000)%100)%10)/1;
		if(h[0]==h[1]&&h[0]==h[2]&&h[0]==h[3]){
			flg=1;
			printf("NA\n");
			break;
		}
		c++;
		for(i=0;i<4;i++){
			p[i]=h[i];
			g[i]=h[i];
		}
		for(i=0;i<3;i++){
			for(j=i+1;j<4;j++){
				if(p[i]>p[j]){
					w=p[i];
					p[i]=p[j];
					p[j]=w;
				}
			}
		}
		for(i=0;i<3;i++){
			for(j=i+1;j<4;j++){
				if(g[i]<g[j]){
					w=g[i];
					g[i]=g[j];
					g[j]=w;
				}
			}
		}
		n=(g[0]*1000+g[1]*100+g[2]*10+g[3])-(p[0]*1000+p[1]*100+p[2]*10+p[3]);
	}
		if(flg!=1){
			printf("%d\n",c);
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int a,s,d[4],f,g,h,i,j,kai,high,low;
	while(1){
		scanf("%d",&a);
		if(a==0000){
			break;
		}
		kai=0;
		s=a;
		h=s;
		while(1){

			if(h==6174){
				printf("%d\n",kai);
				break;
			}
			for(i=0;i<4;i++){
				d[i]=s%10;
				s=s/10;
			}
			if(d[0]==d[1]&&d[1]==d[2]&&d[2]==d[3]){
				printf("NA\n");
				break;
			}
			sort(d,d+4);
			for(i=0;i<4;i++)
			//	printf("%d  ",d[i]);
			high=d[3]*1000+d[2]*100+d[1]*10+d[0];
			 low=d[0]*1000+d[1]*100+d[2]*10+d[3];
			h=high-low;
			kai++;
			s=h;
		}
	}
	return 0;
}