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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	string S;
	while(cin >> S){
		int cost = 0;
		string str = S;
		if(S == "6174"){
			cout << cost << endl;
			continue;
		}
		if(S == "0000")break;
		if(StrToInt(S) % 1111 == 0){
			cout << "NA" << endl;
			continue;
		}
		while(1){
			cost++;
			vector<int> v;
			rep(i,4){
				int a = str[i]-'0';
				v.push_back(a);
			}
			sort(v.begin(), v.end(), greater<int>());
			int L = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			sort(v.begin(), v.end());
			int S = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			int sum = L - S;
			cout << "L:" << L << " S:" << S << endl;
			cout << "cost:" << cost << " " << sum << endl;
			if(sum == 6174){
				cout << cost << endl;
				break;
			}
			str = IntToStr(sum);
		}


	}
	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

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

int check(int a){
  vector<int> b;
  for(int i=0; i<4; i++){
    b.push_back(a%10);
    a/=10;
  }
  sort(b.begin(),b.end());
  int p=0, q=0, u=1;
  for(int i=0; i<4; i++){
    q+=b[i]*u;
    p+=b[3-i]*u;
    u*=10;
  }
  return q-p;
}

int main(){
  int t;
  while(true){
    cin >> t;
    if(t==0)
      break;
    bool f = true;
    for(int i=1; i<9; i++)
      if(t==i*1111){
	cout << "NA\n";
	f = false;
      }
    if(f){
      if(t==6174)
	cout << 0 << endl;
      else{
	int kk = 1;
	int pp = check(t);
	while(pp != 6174){
	  pp = check(pp);
	  kk++;
	}
	cout << kk << endl;   
      }   
    }
  }
  return 0;
}

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

char N[4], n[4];
int L, S, d;

int main()
{
  while(true)
  {
    cin >> N;
    if(N[0] == '0' && N[1] == '0' && N[2] == '0' && N[3] == '0')
      break;
    if(N[0] == N[1] && N[1] == N[2] && N[2] == N[3])
    {
      cout << "NA/n";
      continue;
    }
    for(int i = 0; i < 10; i++)
    {
      if(N[0] == '6' && N[1] == '1' && N[2] == '7' && N[3] == '4')
      {
        cout << i << endl;
        break;
      }

      L = S = 0;
      sort(N, N + 4);
      for(int j = 0; j < 4; j++)
        S += (N[j] - '0') * pow(10, 3 - j);
      reverse(N , N + 4);
      for(int j = 0; j < 4; j++)
        L += (N[j] - '0') * pow(10, 3 - j);
      d = L - S;
      for(int j = 0; j < 4; j++)
      {
        N[j] = (d / (int)pow(10, 3 - j)) + '0';
        d -= (N[j] - '0') * (int)pow(10, 3 - j);
      }
    }
  }
}

C++: #include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<set>
#include<stack>
#include<cstdio>
using namespace std;
char n[5];
int k[5];

int jk(int sum); // L-Sを返す
int j;

int main(){
  int cnt;
  int sum;
  int fl;
  while(1){

    scanf("%s",n);

    if(n[0]=='0'&&n[1]=='0'&&n[2]=='0'&&n[3]=='0')break;
    fl = 0;
    for(int i=0;i<3;i++){
      if(n[i] != n[i+1]){
        fl = 1;
      }
    }

    //分解
    if(fl == 1){
      sum = 0;
      j = 1000;
      for(int i=0;i<4;i++){
        k[i] = n[i]-'0';
        sum+= k[i]*j;
        j/=10;
      }

      //操作
      cnt = 0;
      while(sum != 6174){
        cnt++;
        sum = jk(sum);
      }
      cout << cnt << endl;
    }else{
      cout << "NA" << endl;
    }
  }

  return 0;
}

int jk(int sum){
  int ls,l=0,s=0;
  int j;
  j = 1000;
  for(int i=0;i<4;i++){
    k[i] = sum/j;
    sum = sum%j;
    j/=10;
  }
  sort(k,k+4);
  j = 1000;
  for(int i=0;i<4;i++){
    s+=k[i] * j;
    j/=10;
  }

  reverse(k,k+4);
  j = 1000;
  for(int i=0;i<4;i++){
    l+=k[i] * j;
    j/=10;
  }

  ls = l - s;

  return ls;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;



/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/



int main()
{
	for (char s[5]; scanf("%s", s), strcmp(s, "0000")!=0;)
	{
		if (count(s, s + 4, s[0]) == 4)
		{
			cout << "NA" << endl;
			continue;
		}
		
		int ans = 0;
		while (strcmp(s, "6174") != 0)
		{
			char s1[5];
			strcpy(s1, s);
			sort(s1, s1 + 4);
			char s2[5];
			strcpy(s2, s);
			sort(s2, s2 + 4, greater<char>());
			int a1 = atoi(s1);
			int a2 = atoi(s2);
			sprintf(s, "%04d", a2 - a1);
			ans++;
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int d[4];
int main(){
    int a;
    while(scanf("%d",&a),a){
        if(a%1111==0){
            printf("NA\n");
            continue;
        }
        int count=0;
        while(a!=6174){
            for(int i=0;i<4;i++){
                d[i]=a%10;
                a/=10;
            }
            std::sort(d,d+4);
            int b=0;
            for(int i=0;i<4;i++){
                b*=10;
                b+=d[i];
            }
            int c=0;
            for(int i=3;i>=0;i--){
                c*=10;
                c+=d[i];
            }
            a=c-b;
            count++;
        }
        printf("%d\n",count);
    }
}

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

int main(){
  string s;

  while(cin >> s, s != "0000"){
    if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
      cout << "NA" << endl;
      continue;
    }

    int ans = 0;

    while(s != "6174"){
      sort(s.begin(), s.end());
      string S = s;
      sort(s.rbegin(), s.rend());
      string L = s;
      int N = atoi(L.c_str()) - atoi(S.c_str());

      char buff[10];
      sprintf(buff, "%04d", N);
      buff[4] = 0;
      s = string(buff);

      ans++;
    }

    cout << ans << endl;
  }
}