Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		count := 0
		for v != 6174 {
			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))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

C++: // すべての数は6174に通ず.cpp : Defines the entry point for the console application.
//
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void sort(int a[]) {
	for (int i = 1; i < 4; i++) {
		int v = a[i];
		int j = i - 1;
		while (j >= 0 && a[j] > v) {
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = v;
	}
}

int main(void)
{
	int result[10000], suffix = 0;

	while (1) {
		int n, temp[4], save, big, small, count = 0;

		scanf("%d", &n);
		save = n;
		if (n == 0) {
			break;
		}
		bool success = false;
		for (int i = 0; i < 4; i++) {
			temp[i] = n % 10;
			n /= 10;
		}
		n = save;
		for (int i = 0; i < 3; i++) {
			if (temp[i] != temp[i + 1]) {
				success = true;
				break;
			}
		}

		while (n != 6174 && success == true) {
			for (int i = 0; i < 4; i++) {
				temp[i] = n % 10;
				n /= 10;
			}

			sort(temp);

			small = 0;
			big = 0;
			for (int i = 0; i < 4; i++) {
				small = small * 10 + temp[i];
				big = big * 10 + temp[3 - i];
			}
			n = big - small;
			count++;
		}

		if (success == true) {
			result[suffix] = count;
		}
		else {
			result[suffix] = -1;
		}
		suffix++;
	}

	for (int i = 0; i < suffix; i++) {
		if (result[i] == -1) {
			puts("NA");
		}
		else {
			printf("%d\n", result[i]);
		}
	}

	return 0;
}

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

signed main()
{
  int N;
  while (cin >> N, N) {
    if (!(N % 1111)) cout << ("NA");
    else {
      int cnt = 0;
      while (N != 6174) {
        int n[4], L = 0, S = 0;
        for (int i = 0; i < 4; i++) {
          n[i] = N % 10;
          N /= 10;
        }
        sort(n, n + 4);
        for (int i = 0; i < 4; i++) {
          L = L * 10 + n[3 - i];
          S = S * 10 + n[i];
        }
        cnt += 1;
        N = L - S;
      }
      cout << cnt;
    }
    cout << "\n";
  }

  return (0);
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<Windows.h>
using namespace std;
int main(){
	char num[6]={0};
	
	while(1){
		int cnt = 0;
		scanf("%s",num);
		if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
			if(num[0] == 48)break;
			else puts("NA");
			continue;
		}
		if(num[0] == '6' && num[1] == '1' && num[2] == '7' && num[3] == '4'){
			puts("0");
			continue;
		}
		while(1){
			sort(num,num+4);
			int s = atoi(num);
			reverse(num,num+4);
			int l = atoi(num);
			int x = 0;
			if(l - s == 6174){
				printf("%d\n",cnt+1);
				break;
			}
			else{
				x = l - s;
				cnt++;
				for(int i = 0; i < 4; i++){
					num[i] = (x / pow(10,3-i))+48;
					x -= (num[i]-48) * pow(10,3-i);
				}
			}
		}
		
	}
	return 0;
}

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

int main() {
	std::string str;
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		std::string l, s;
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			l = str;
			std::sort(l.begin(), l.end(), std::greater<char>());
			s = str;
			std::sort(s.begin(), s.end());
			str = std::to_string(std::stoi(l) - std::stoi(s));
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <deque>
#include <map>

#define ll long long
#define ull unsigned long long
#define mod 1000000007

using namespace std;

int main() {
	while(true){
		char x[4];
	for (int i = 0; i < 4; ++i) {
		cin >> x[i];
		x[i] -= '0';
	}

	if (x[0] == x[1] && x[1] == x[2] && x[2] == x[3]) {
		if(x[0] == 0) break;
		else cout << "NA" << "\n";
		continue;
	}

	int counter = 0;
	int t = x[0] * 1000 + x[1] * 100 + x[2] * 10 + x[3];
	while (t != 6174) {
		++counter;
		sort(x, x + 4);
		t = x[3] * 999 + x[2] * 90 - x[1] * 90 - x[0] * 999;
		x[0] = t / 1000;
		x[1] = (t / 100) - x[0] * 10;
		x[2] = (t / 10) - x[0] * 100 - x[1] * 10;
		x[3] = t - x[0] * 1000 - x[1] * 100 - x[2] * 10;
	}

	cout << counter << "\n";
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int dp[10000];
int solve(int num){
    if(num==6174){
        return 0;
    }else if(dp[num]){
        return dp[num];
    }else{
        int x[4];
        x[0]=num%10;
        x[1]=(num%100-num%10)/10;
        x[2]=(num%1000-num%100)/100;
        x[3]=(num%10000-num%1000)/1000;
        sort(x,x+4);
        dp[num]=solve(x[3]*999+x[2]*90-x[0]*999-x[1]*90)+1;
        return dp[num];
    }
    
}
int main(void){
   
 
   int N;
   while(1){
       cin>>N;
        
       if(N==0)break;
       
       if(N==1111||N==2222||N==3333||N==4444||N==5555||N==6666||N==7777||N==8888||N==9999){
           cout<<"NA"<<endl;
       }else{
           cout<<solve(N)<<endl;
       }
       
      
   }

}

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

int main()
{
    string N;
    while ( cin >> N, N != "0000" ) {
        int cnt = 0;
        set < string > st;

        while ( N != "6174" && st.find(N) == st.end() ) {
            string L = N, S = N;
            int val = 0;
            st.insert(N);
            sort(rbegin(L), rend(L));
            sort(begin(S), end(S));
            val = atoi(L.c_str()) - atoi(S.c_str());
            N = to_string(val);
            while ( N.size() < 4 ) {
                N = '0' + N;
            }
            cnt++;
        }

        if ( N == "6174" ) {
            cout << cnt << endl;
        } else {
            cout << "NA" << endl;
        }
    }

    return ( 0 );
}

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

bool isSameNumber(vector<int> n){
    bool isSame = true;
    for(int i=0; i < n.size(); i++){
        for(int j=0; j < n.size(); j++){
            if(n[i] != n[j])isSame = false;
        }
    }
    return isSame;
}

int main(){
    vector<int> n;
    int input;
    while(cin >> input){
        int ans = 0;
        bool isok = true;
        if(input == 0) break;
        if(input % 1111 == 0){
            cout << "NA" << endl;
            continue;
        }
        while(input != 6174) {
            if(log10(input) < 3) n.push_back(0);
            while(input != 0){
                n.push_back(input%10);
                input /= 10;
            }
            vector<int> minNum ,maxNum;

            sort(n.begin(), n.end());
            minNum = n;
            reverse(n.begin(), n.end());
            maxNum = n;

            int p=0, q=0;
            for(int i=0, m = 1000; i < 4; i++, m/=10){
                p += minNum[i]*m;
                q += maxNum[i]*m;
            }
            input = q-p;
            ans++;

            n.clear();
        }
        if(isok)cout << ans << endl;
    }
}

C++: #include <iostream>
#include <algorithm> 
using namespace std;
int main(){
	int N; 
	int n[4];
	while(true){
		int cnt=0;
		cin >> N;
		if(N/1000==0) break;
		if(N%1111==0) {
			cout << "NA\n";
			continue;
		}
		while(N!=6174){
			n[0]=N/1000;
			n[1]=(N%1000)/100;
			n[2]=((N%1000)%100)/10;
			n[3]=((N%1000)%100)%10;
			sort(n,n+4);
			int L = n[3]*1000+n[2]*100+n[1]*10+n[0];
			int S = n[0]*1000+n[1]*100+n[2]*10+n[3];
			N=L-S;
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}