ababup1192
6/14/2017 - 2:38 PM

0614.c

#include <stdio.h>

int def(int n, int count){
    int i,j,a[4],l,s,k;

    if(n==6174){
        return count;
    }else{

        a[3]=n/1000;
        a[2]=(n/100)%10;
        a[1]=(n/10)%10;
        a[0]=n%10;

        for(i=0;i<3;i++){
            for(j=0;j<3-i;j++){
                if(a[j]<a[j+1]){
                    k=a[j];
                    a[j]=a[j+1];
                    a[j+1]=k;
                }
            }
        }

        l=a[0]*1000 + a[1]*100 + a[2]*10 + a[3];
        s=a[3]*1000 + a[2]*100 + a[1]*10 + a[0];

        return def(l-s, count + 1);
    }
}

int main(){
    int num,ans;
    while(scanf("%d",&num),num){
        if(num%1111==0){
            printf("NA\n");
        }else{
            printf("%d\n", def(num, 0));
        }
    }

    return 0;
}