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++: #include<stdio.h>
int main(void)
{
	int n,h[4],s[4],k[4],i,j,temp,flg,cnt;
	while(1){
		cnt=0;
		scanf("%d",&n);
		if(n==0000)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;
		while(1){
			if(h[0]==h[1]&&h[0]==h[2]&&h[0]==h[3]){
			printf("NA\n");
			break;
			}
			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;
				cnt++;
			for(i=0;i<4;i++){
				s[i]=h[i];
				k[i]=h[i];
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(s[i]>s[j]){
						temp=s[i];
						s[i]=s[j];
						s[j]=temp;
					}
				}
				for(j=i+1;j<4;j++){
					if(k[i]>k[j]){
						temp=k[i];
						k[i]=k[j];
						k[j]=temp;
					}
				}
			}
		}
		if(cnt>0){
			printf("%d\n",cnt);
		}
	}
	return 0;
}

C++: #include<cstdio>
using namespace std;
int main()
{
	int n,s,i,j,cnt,a[4],b,x,y,flg;
	while(1){
		scanf("%d",&n);
		if(n==0){
			break;
		}
		flg=0;
		cnt=0;
		while(1){
			if(n==6174){
					printf("%d\n",cnt);
					break;
				}
			a[0]=n/1000;
			n%=1000;
			a[1]=n/100;
			n%=100;
			a[2]=n/10;
			a[3]=n%10;
			//printf("%d %d %d %d\n",a[0],a[1],a[2],a[3]);
			if(a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0){
				flg=1;
				break;
			}
			else if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]){
				printf("NA\n");
				break;
			}
			for(i=0;i<4-1;i++){
				for(j=i+1;j<4;j++){
					if(a[i]<a[j]){
						b=a[i];
						a[i]=a[j];
						a[j]=b;
					}
				}
			}
			y=a[0]*1000+a[1]*100+a[2]*10+a[3];
			x=a[3]*1000+a[2]*100+a[1]*10+a[0];
			n=y-x;
			//printf("x=%d y=%d n=%d\n",x,y,n);
			cnt++;
		}
		if(flg==1){
			break;
		}
	}
	return 0;
}

C++: // 2013/07/07 Tazoe

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	while(true){
		int N;
		cin >> N;

		if(N==0)
			break;

		if(N%1111==0){
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;

		while(true){
			if(N==6174){
				cout << cnt << endl;
				break;
			}

			int n[4];
			n[0] = N/1000;
			n[1] = N/100%10;
			n[2] = N/10%10;
			n[3] = N%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++;
		}
	}

	return 0;
}

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

using namespace std;

int func2(int *temp) {
    int n = 0;
    
    n += (temp[0] * 1000);
    n += (temp[1] * 100);
    n += (temp[2] * 10);
    n += temp[3];
       
    return n;
}

int main() {
    while (true) {
        int N = 0, Nm[4] = {0};
        unsigned int count = 0;
        int judge = 0;
        int Lm[4] = {0}, Sm[4]={0};
        int S = 0, L = 0;
        
	cin >> N;
        
        Nm[0] = N / 1000;
        N %= 1000;
        Nm[1] = N / 100;
        N %= 100;
        Nm[2] = N / 10;
        N %= 10;
        Nm[3] = N;
        
        if((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))          break;
        
        for(int i = 0; i < 4; i++) {
            if (Nm[i] == Nm[0]) judge++;
        }
        
        if (judge != 4) {
            while (true) {
                if ((Nm[0] == 6 && Nm[1] == 1) && (Nm[2] == 7 && Nm[3] == 4)) break;
                
                //Sを作成
                sort(Nm, Nm + 4);
                for (int i = 0; i < 4; i++) {   
                    Sm[i] = Nm[i];
                    //cout << Sm[i] << endl;
                }   
                S = func2(Sm);

                //Lを作成
                sort(Nm, Nm + 4, greater<int>());
                for (int i = 0; i < 4; i++) {
                    Lm[i] = Nm[i];
                }
                L = func2(Lm);

                N = L - S;
                count++;

                Nm[0] = N / 1000;
                N %= 1000;
                Nm[1] = N / 100;
                N %= 100;
                Nm[2] = N / 10;
                N %= 10;
                Nm[3] = N;
            }
            cout << count << endl;
        }
        
        if(judge == 4) {
            cout << "NA" << endl;
        }
    }
    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

int func(string s)
{
    for(int i=0;; ++i){
        if(s == "6174")
            return i;
        
        string L = s;
        sort(L.rbegin(), L.rend());
        sort(s.begin(), s.end());
        
        char t[16];
        sprintf(t, "%04d", stoi(L) - stoi(s));
        s = t;
    }
}

int main()
{
    string s;
    while(cin >> s, s != "0000"){
        if(stoi(s)%1111 == 0){
            cout << "NA" << endl;
        }
        else{
            cout << func(s) << endl;
        }
    }
}

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

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	while(cin >> n && n){

		int cnt = 0;
		while(n != 6174){
			cnt++;
			int a[4];
			for(int i = 0; i < 4; ++i){
				a[i] = n % 10;
				n /= 10;
			}

			sort(a, a + 4);
			if(a[0] == a[3]){
				cout << "NA" << endl;
				cnt = -1;
				break;
			}

			int l = 0;
			for(int i = 3; i >= 0; --i){
				l *= 10;
				l += a[i];
			}
			//cout << l << endl;

			int s = 0;
			for(int i = 0; i < 4; ++i){
				s *= 10;
				s += a[i];
			}
			//cout << s << endl;

			n = l - s;

			//cout << n << endl;
		}

		if(cnt != -1) cout << cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int main(){
	while(1){
		int n;
		int a[4]={1000,100,10,1};
		int l[4]={0},s[4]={0};
		int ans[4];
		cin>>n;
		if(n==0000)break;
		int cnt=0;
		int i,j;
		for(i=0;i<4;i++){
			l[i]=n/a[i];
			s[i]=l[i];
			n%=a[i];
		}
		if(l[0]==l[1]&&l[1]==l[2]&&l[2]==l[3])cout<<"NA"<<endl;
		else if(l[0]==6&&l[1]==1&&l[2]==7&&l[3]==4)cout<<0<<endl;
		else{
			while(1){
			for(i=0;i<4;i++){
				for(j=3;j>i;j--){
					if(l[i]<l[j]){
						swap(l[i],l[j]);
					}
					if(s[i]>s[j]){
						swap(s[i],s[j]);
					}
				}
			}
			cnt++;
				n=(l[0]*a[0]+l[1]*a[1]+l[2]*a[2]+l[3]*a[3])-(s[0]*a[0]+s[1]*a[1]+s[2]*a[2]+s[3]*a[3]);
						for(i=0;i<4;i++){
			l[i]=n/a[i];
			s[i]=l[i];
			ans[i]=l[i];
			n%=a[i];
		}
			if(ans[0]==6&&ans[1]==1&&ans[2]==7&&ans[3]==4){
				cout<<cnt<<endl;
				break;
			}
		}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	char str[4];
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		char l[4], s[4];
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			std::memcpy(l, str, 4);
			std::memcpy(s, str, 4);
			std::sort(l, l + 4, std::greater<char>());
			std::sort(s, s + 4);
			int a = atoi(l) - atoi(s);
			itoa(a, str, 10);
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}