Problem:
すべての数は６１７４に通ず
0 から 9 の数字からなる四桁の数 N に対して以下の操作を行う。
N の桁それぞれの数値を大きい順に並べた結果得た数を L とする
N の桁それぞれの数値を小さい順に並べた結果得た数を S とする
差 L-S を新しい N とする(1回分の操作終了)
新しい N に対して 1. から繰り返す
このとき、全桁が同じ数字(0000, 1111など)である場合を除き、あらゆる四桁の数はいつかは 6174になることが知られている。例えば N = 2012の場合
1回目 (N = 2012): L = 2210, S = 0122, L-S = 2088
2回目 (N = 2088): L = 8820, S = 0288, L-S = 8532
3回目 (N = 8532): L = 8532, S = 2358, L-S = 6174
となり、3 回の操作で 6174 に到達する。
0 から 9 の数字からなる四桁の数が与えられたとき、何回の操作で 6174 に到達するか計算するプログラムを作成して下さい。
入力
入力は複数のデータセットからなる。入力の終わりは 0000 が１つの行で示される。各データセットは以下の形式で与えられる。
N
データセットは１行であり、N (1 ≤ N ≤ 9999) は四桁の数を示す。N < 1000 の場合は上の桁は 0 で埋められている。
データセットの数は 10000 を超えない。
出力
各データセットごとに何回の操作で 6174 に到達したかを１行に出力する。ただし全桁が同じ数字である数が入力として与えられた場合は NA と出力する。
入力例
6174
2012
3333
0000
出力例
0
3
NA

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX 1000000007

int main(void){
    string s;
    while(true){
        cin >> s;
        if(s == "0000") break;
        if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
            cout << "NA" << endl;
            continue;
        }
        int ans = 0,l = 0;
        while(s != "6174"){
            sort(s.begin(),s.end());
            l = stoi(s);
            reverse(s.begin(),s.end());
            l = l*-1 + stoi(s);
            ans++;
            s = to_string(l);
            rep(i,4-s.size()) s='0'+s;
        }
        cout << ans << endl;
        
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 7

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

#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define Rrep(i,n) for(int i=n-1;i>=0;i--)
#define Rrep1(i,n) for(int i=n;i>0;i--)
#define all(a) a.begin(),a.end()
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
#define INF ((ll)4e18)
#define IINF ((int)1e9)
const double PI = 3.141592653589793;//2384626433832795028841971;
template<typename T>inline bool chmax(T& a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T>inline bool chmin(T& a, T b) { return ((a > b) ? (a = b, true) : (false)); }
#define yesno(ans) cout<<((ans)?"Yes\n":"No\n")
bool eq(double a, double b) { return abs(a - b) < 0.0000001; }
//LRUD
const int di[4] = { 0,-1,0,1 };
const int dj[4] = { -1,0,1,0 };
//const int di[8] = { 0,0,1,1,1,-1,-1,-1 };
//const int dj[8] = { 1,-1,0,-1,1,0,-1,1 };

/*ACL
#include <atcoder/all>
using namespace atcoder;
//using mint=modint;
using mint = modint998244353;
//using mint=modint1000000007;
using vm=vector<mint>;
using vvm=vector<vm>;
using vvvm=vector<vvm>;
//*/


int solve(){
    int N=9999;
    int M=9999;
    for(;cin>>N,N;){
        int ans=0;
        bool isNA=0;
        while(N!=6174){
            ans++;
            string L=to_string(N);
            while(L.size()<4)L+='0';
            string S=L;
            sort(all(L));
            reverse(all(L));
            sort(all(S));
            N=stoi(L)-stoi(S);
            if(N==0){
                isNA=1;
                break;
            }
        }
        if(isNA){
            cout<<"NA"<<endl;
            continue;
        }
        cout<<ans<<endl;
    }
    return 0;
}

int main()
{
    //std::cin.tie(0)->sync_with_stdio(0);
    int T = 1;
    //cin>>T;
    while (T--)
    {
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3500
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string N;
    while(cin>>N){
        if(N=="0000")return 0;
        ll cnt=0;
        unordered_set<string> SS;
        while(N!="6174"){
            if(SS.count(N)){
                cout<<"NA"<<endl;
                cnt=-1;
                break;
            }
            SS.insert(N);
            string S=N,T=N;
            sort(all(S));
            sort(all(T));
            reverse(all(T));
            N=to_string(stoll(T)-stoll(S));
            while(N.size()<4)N='0'+N;
            cnt++;
        }
        if(cnt>=0)cout<<cnt<<endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3524
final_rank: 14

code_snippet: #include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
ll n,i,j,k,ans,a[4],t,minm,maxm;
int main()
{
	while(cin>>n&&n!=0000) {
		ans=0;
		for(i=0;;i++) {
			
			if(n==6174) break;
			k=n;
			for(j=0;j<=3;j++) {
				t=k%10;
				k=k/10;
				a[j]=t;
			}
			sort(a,a+4);
			if(a[0]==a[3]) break;
			minm=0;maxm=0;
			for(j=0;j<=3;j++) {
				minm+=a[j]*pow(10,3-j);
				maxm+=a[j]*pow(10,j);
				
				/*——————————————————————*/
			}
			ans++;
			n=maxm-minm;
		}
		if(n==6174)
		cout<<ans<<endl;
		else 
		cout<<"NA"<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3284
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#include <string>
#define rep(i,n) for(int i = 0; i < n; i++)
#define rrep(i,a,b) for(int i = a; i < b; i++)
#define all(A) (A).begin(),(A).end()
#define MOD 1000000007
#define grt greater<int>()
#define mapf(M) for(auto& [k,v] : M)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
ll INF = 1LL<<60;
typedef vector<vector<ll> > Graph;

int main() {
	while(1){
		string s; cin >> s;
		int n = stoi(s);
		if(n == 0) return 0;
		bool same = 1;
		rep(i,3) if(s[0] != s[i+1]) same = 0;
		if(same){
			cout << "NA\n";
			continue;
		}

		int cnt = 0;
		while(n != 6174){
			sort(all(s)); int ls = stoi(s);
			sort(all(s),greater<char>()); int ss = stoi(s);
			n = ss-ls;
			s = to_string(n);
			rep(i,4-(int)s.size()) s = '0'+s;
			cnt++;
		}
		cout << cnt << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3492
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
int solve(int n) {
    vector<int>v;

    v.push_back(n % 10);
    n /= 10;
    v.push_back(n % 10);
    n /= 10;
    v.push_back(n % 10);
    n /= 10;
    v.push_back(n % 10);
    n /= 10;

    sort(v.begin(), v.end());
    int p1 = 0;
    for (int i = 0; i < v.size(); i++) {
        p1 *= 10;
        p1 += v[i];
    }
    int p2 = 0;
    for (int i = v.size() - 1; i >= 0; i--) {
        p2 *= 10;
        p2 += v[i];
    }
    return p2 - p1;
}
int main(){
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        int i = 0;
        while (1) {
            if (n == 6174 ) {
                cout << i << endl;
                break;
            }
            n = solve(n);
            i++;
            if (i > 10000) {
                cout << "NA" << endl;
                break;
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3444
final_rank: 6

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

int main() {
    while (true) {
        string N;
        cin >> N;
        if(N == "0000") {
            return 0;
        }
        if(N == "6174") {
            cout << 0 << endl;
            continue;
        }
        int ans = 0;
        while (true) {
            ans++;
            string A = N,B = N;
            sort(A.rbegin(),A.rend());
            sort(B.begin(),B.end());
            string C = to_string(stoi(A)-stoi(B));
            N = string(4-C.size(),'0')+C;
            if(N == "0000") {
                cout << "NA" << endl;
                break;
            }
            if(N == "6174") {
                cout << ans << endl;
                break;
            }
        }
    }
    
}
accuracy: 1
time_ms: 10
memory_kb: 3488
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  while (true){
    int N;
    cin >> N;
    if (N == 0){
      break;
    }
    if (N % 1111 == 0){
      cout << "NA" << endl;
    } else {
      int cnt = 0;
      while (N != 6174){
        string S = to_string(N);
        while (S.size() < 4){
          S.insert(S.begin(), '0');
        }
        sort(S.begin(), S.end());
        int X = stoi(S);
        reverse(S.begin(), S.end());
        int Y = stoi(S);
        N = Y - X;
        cnt++;
      }
      cout << cnt << endl;
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3460
final_rank: 8

code_snippet: #include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int count_op(string str) {
  int count = 0;
  int num;
  string L, S;

  if (str == "6174") return 0;
  
  do {
    L = str;
    S = str;
    sort(L.begin(), L.end(), greater<char>());
    sort(S.begin(), S.end());
    
    num = stoi(L) - stoi(S);
    if (num < 1000) {
      str = "0" + to_string(num);
    } else {
      str = to_string(num);
    }
    count++;
  } while (num != 6174);
  
  return count;
}

int main()
{
  int count;
  string N;

  while (1) {
    cin >> N;

    if (N == "0000") {
      break;
    }
    if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
      cout << "NA" << endl;
    } else {
      count = count_op(N);
      cout << count << endl;
    }
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3388
final_rank: 5

code_snippet: #include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
#include<sstream>
#include<random>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int inf=mod*mod;
signed main(){
	int n;
	while(cin>>n,n){
		for(int i=0;;i++){
			if(n%1111==0){
				cout<<"NA"<<endl;
				break;
			}
			if(n==6174){
				cout<<i<<endl;
				break;
			}
			string s=to_string(n);
			s=string(4-s.size(),'0')+s;
			sort(s.begin(),s.end());
			string l=s;
			reverse(l.begin(),l.end());
			n=stoi(l)-stoi(s);
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3512
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b; 
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main(){
  //cin.tie(0);	
  //ios::sync_with_stdio(false);
  while(1){
    string n;
    cin>>n;
    if(n=="0000") break;
    int ans=0;
    if(n[0]==n[1] && n[1]==n[2] && n[2]==n[3]){
      cout<<"NA"<<endl;
      continue;
    }
    int cnt=0;
    while(n!="6174"){
      // cout<<n<<endl;
      string mn,mx;
      ans++;
      sort(n.begin(),n.end(),greater<char>() );
      mx=n;
      sort(n.begin(),n.end() );
      mn=n;
      //cout<<mx<<" "<<mn<<endl;
      int x=stoll(mx),y=stoll(mn);
      x=x-y;
      n=to_string(x);
      while(n.size() < 4) n="0"+n;
    }
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3512
final_rank: 12

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


// {{{

// clang-format off
#define _over_load(_1,_2,_3,_4,NAME,...) NAME
#define range(...) _over_load(__VA_ARGS__, range4, range3, range2)(__VA_ARGS__)
#define range2(i, r) for ( int i = 0; i < (int)(r); (i) += 1)
#define range3(i, l, r) for ( int i = (int)(l); i < (int)(r); (i) += 1)
#define range4(i, l, r, inc) for ( int i = (int)(l); i < (int)(r); (i) += (inc))
#define rrange(...) _over_load(__VA_ARGS__, rrange4, rrange3, rrange2)(__VA_ARGS__)
#define rrange2(i, r) for ( int i = (int)(r) - 1; i >= 0; (i) -= 1)
#define rrange3(i, l, r) for ( int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)
#define rrange4(i, l, r, inc) for ( int i = (int)(r) - 1; i >= (int)(l); (i) -= inc)
#define debug(...) debug_func(#__VA_ARGS__, __VA_ARGS__)
template <typename T, typename... T2> void debug_func(string_view name, const T &a, T2 &&...rest) { stack<char> bs; string_view lbs = "({[<"; string_view rbs = ")}]>"; int end = name.size(); for ( int i = 0; i < (int)name.size(); i++ ) { if ( lbs.find(name[i]) != string::npos ) { bs.push(name[i]); } if ( rbs.find(name[i]) != string::npos ) { if ( !bs.empty() ) { bs.pop(); } } if ( name[i] == ',' && bs.empty() ) { end = i; break; } } cerr << name.substr(0, end) << ":" << a; if constexpr ( sizeof...(rest) == 0 ) { cerr << endl; } else { cerr << ' '; debug_func(name.substr(name.find_first_not_of(' ', end + 1)), forward<T2>(rest)...); } }
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << ' ' << p.second; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { for ( pair<T1, T2> x : v ) { os << x << "\n"; } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> v) { if(!v.empty()) { os << v.front(); v.pop(); } while (!v.empty()) { os << " " << v.front(); v.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> v) { if(!v.empty()) { os << v.top(); v.pop(); } while (!v.empty()) { os << " " << v.top(); v.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
using ull = unsigned long long;
using ll = long long;
using Pll = pair<ll, ll>;
using P = pair<int, int>;
constexpr ll INF64 = INT64_MAX / 2;
constexpr int INF32 = INT32_MAX / 2;
constexpr int dy[] = { 0, -1, 1, 0, -1, 1, -1, 1 };
constexpr int dx[] = { -1, 0, 0, 1, -1, -1, 1, 1 };
constexpr int mod998244353 = 998244353;
constexpr int mod1000000007 = (int)1e9 + 7;
constexpr char newl = '\n';
// clang-format on

// }}}


int ans=0;
void dfs(string s){
  if(s=="6174"){
    cout<<ans<<endl;
    return;
  }

  string high=s,low=s;
  sort(high.rbegin(),high.rend());
  sort(low.begin(),low.end());

  ostringstream scout;
  scout << setfill('0') << setw(4) << to_string(stoi(high)-stoi(low));
  s=scout.str();

  ans++;
  dfs(s);
}

void solve(string s){
  ans=0;
  dfs(s);
}

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

  while(1){
    string n;
    cin>>n;
    if(n=="0000") break;

    bool is_NA=false;
    range(i,1,10){
      char c='0'+i;
      string same="";
      range(j,4) same.push_back(c);
      if(n==same){
        is_NA=true;
        break;
      }
    }
    if(is_NA){
      cout<<"NA"<<endl;
      continue;
    }
    solve(n);
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3528
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    string s,t;
    while(cin>>s){
        if(s=="0000")return 0;
        if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
            out("NA");
            continue;
        }
        int ans=0;
        while(s!="6174"){
            ans++;
            t=s;
            sort(rall(s));
            sort(all(t));
            s=to_string(stoi(s)-stoi(t));
            while(s.size()<4){
                reverse(all(s));
                s.push_back('0');
                reverse(all(s));
            }
        }
        out(ans);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3284
final_rank: 3

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void solve(string N){
if ( N[0] == N[1] && N[1] == N[2] && N[2] == N[3] ) {
cout << "NA" << endl; return;
}
int cnt = 0;
string L, S;
for(; N != "6174"; cnt++ ){
L = S = N;
sort(L.begin(), L.end()); // 昇順にソート
reverse(L.begin(), L.end()); // 逆順にして降順にソート
sort(S.begin(), S.end()); // 昇順にソート
int num = stoi(L.c_str()) - stoi(S.c_str());
N = to_string(num);
while( N.size() < 4 ) N = '0' + N;
}
cout << cnt << endl;
}
int main(){
string N;
while(1){
cin >> N;
if ( N == "0000" ) break;
solve(N);
}
return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3236
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

int main(){
    while(1){
        string s;
        cin >> s;
        if(s == "0000") break;

        if(s[0]==s[1] and s[0]==s[2] and s[0]==s[3]){
            cout << "NA" << endl;
            continue;
        }
        int count = 0;
        while(stoi(s) != 6174){
            sort(s.begin(), s.end());
            int b = stoi(s);
            reverse(s.begin(), s.end());
            int a = stoi(s);
            ostringstream sout;
            sout << setfill('0') << setw(4) << a-b;
            s = sout.str();
            count++;
        }
        cout << count << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3172
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
int r[4]={6,1,7,4};
bool c(int n[]){
	for(int i=0; i<4; i++){
		if(r[i]!=n[i]){
			return false;
		}
	}
	return true;
}
bool c2(int n[]){
	for(int i=0; i<3; i++){
		if(n[i]!=n[i+1]){
			return false;
		}
	}
	return true;
}
bool c3(int n[]){
	for(int i=0; i<4; i++){
		if(n[i]!=0){
			return false;
		}
	}
	return true;
}
int main(){
	while(true){
		int q=0;
		char a[4];
		int n[4]={};
		cin>>a;
		if(a=="0000") break;
		n[0]=int(a[0])-int('0');
		n[3]=int(a[3])-int('0');
		n[2]=int(a[2])-int('0');
		n[1]=int(a[1])-int('0');
		bool t=true;
		if(c3(n)) break;
		while(true){
			if(c(n))break;
			if(c2(n)){ 
				t=false; break;
			}
			sort(n,n+4);
			int s=0,l=0,sk=1000,lk=1;
			for(int i=0; i<4; i++){
				s+=sk*n[i];sk/=10;
				l+=lk*n[i];lk*=10;
			}
			int m=l-s,k=1000;
			for(int i=0; i<4; i++){
				n[i]=m/k;
				m-=n[i]*k;
				k/=10;
			}
			q++;
		}
		if(t) cout<<q<<endl;
		else cout<<"NA"<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3176
final_rank: 1

code_snippet: #include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin Headelr {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

#define all(x) x.begin(), x.end()
#define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i)
#define reps(i, b, e) for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

constexpr size_t    operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t  operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };

constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;

template <class T, class Compare = less<>>
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template <class T, class Compare = greater<>>
using MinHeap = priority_queue<T, vector<T>, Compare>;

using imax = intmax_t;
using usize = size_t;

inline void input() {}
template <class Head, class... Tail>
inline void input(Head&& head, Tail&&... tail) {
    cin >> head;
    input(forward<Tail>(tail)...);
}

template <class T>
inline istream& operator>>(istream &is, vector<T> &vec) {
    for (auto &e: vec) {
        is >> e;
    }
    return is;
}

inline void output() { cout << "\n"; }
template <class Head, class... Tail>
inline void output(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) {
        cout << " ";
    }
    output(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<<(ostream &os, const vector<T> &vec) {
    static constexpr const char *delim[] = {" ", ""};
    for (const auto &e: vec) {
        os << e << delim[&e == &vec.back()];
    }
    return os;
}

template <class T>
inline vector<T> makeVector(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeVector(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(makeVector<T>(initValue, args...))>(sz, makeVector<T>(initValue, args...));
}

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class Container>
struct reverse_t {
    Container &c;
    reverse_t(Container &c) : c(c) {}
    auto begin() { return c.rbegin(); }
    auto end() { return c.rend(); }
};

template <class Container>
auto reversed(Container &c) {
    return reverse_t<Container>(c);
}

template <class T>
inline bool chmax(T &a, const T &b) noexcept {
    return b > a && (a = b, true);
}

template <class T>
inline bool chmin(T &a, const T &b) noexcept {
    return b < a && (a = b, true);
}

template <class T>
inline T diff(const T &a, const T &b) noexcept {
    return a < b ? b - a : a - b;
}
// }}} End Header

constexpr int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

const auto inside = [](int y, int x, int H, int W) -> bool {
    return (y >= 0 && x >= 0 && y < H && x < W);
};

struct Data {
    imax score;
    usize id;

    Data(imax score, usize id) : score(score), id(id) {}

    bool operator>(const Data& rhs) const {
        if (score == rhs.score) {
            return id < rhs.id;
        } else {
            return score > rhs.score;
        }
    }
};

// SegmentTree {{{
template <class Monoid>
struct SegmentTree {
    using Func = function<Monoid(Monoid, Monoid)>;
    const size_t sz;
    const Func fn;
    const Monoid unity;
    vector<Monoid> seg;

    SegmentTree(const size_t n, const Monoid &u, Func f)
        : sz(1 << (__lg(n + 5) + 1)), fn(f), unity(u), seg(sz * 2, unity) {}

    void set(size_t k, const Monoid &v) {
        seg[k + sz] = v;
    }

    Monoid& operator[](size_t k) {
        return seg[k + sz];
    }
    const Monoid& operator[](size_t k) const {
        return seg[k + sz];
    }

    void build() {
        for (size_t k = sz - 1; k > 0; --k) {
            seg[k] = fn(seg[2 * k], seg[2 * k + 1]);
        }
    }

    void update(size_t k, const Monoid &x) {
        k += sz;
        seg[k] = x;
        while (k >>= 1) {
            seg[k] = fn(seg[2 * k], seg[2 * k + 1]);
        }
    }

    Monoid query(size_t a, size_t b) const {
        Monoid L = unity, R = unity;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = fn(L, seg[a++]);
            if (b & 1) R = fn(seg[--b], R);
        }
        return fn(L, R);
    }
};
// }}}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios_base::fixed);
    cout.precision(10);

    const auto so = [&](string s) -> string {
        sort(all(s));
        return s;
    };
    const auto rso = [&](string s) -> string {
        sort(all(s), greater<>());
        return s;
    };

    imax N;
    while (cin >> N, N) {
        if (N == 6174) {
            output(0);
            continue;
        }
        string s = to_string(N);
        s = string(4 - s.size(), '0') + s;
        if (s == string(4, s[0])) {
            output("NA");
            continue;
        }
        usize res = 0;
        while (s != "6174") {
            const imax L = stoi(rso(s));
            const imax S = stoi(so(s));
            s = to_string(L - S);
            s = string(4 - s.size(), '0') + s;
            res++;
        }
        output(res);
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3276
final_rank: 2

code_snippet: #include<bits//stdc++.h>
#define rep(i,n) for(int i = 0;i <n;i++)
using namespace std;
int main(){
	string s;int b;
	while (cin >> s,s!="0000") {
		if (s[0] == s[1]&& s[2] == s[3]&&s[2]==s[3]) {
			cout << "NA" << endl; continue;
		}int cnt = 0;
		while (s != "6174") {
			int  L = 0, S = 0;
			sort(s.begin(), s.end());
			rep(i, 4) L += (s[i] - '0') * pow(10, i);
			rep(i, 4) S += (s[3 - i] - '0')*pow(10, i);
			b = L - S; cnt++;
			s = to_string(b);
		}cout << cnt << endl;
	}
}
accuracy: 0.75
time_ms: 20
memory_kb: 3424
final_rank: 20

code_snippet: #include<string.h>
#include<iostream>
#include<string>
#include <vector>
#include<algorithm>

using namespace std;

int Func(string str_num)
{
	if (str_num == "6174")
		return 0;
	int a[4];
	for (int i = 0; i < 4; i++)
		a[i] = str_num[i] - '0';
	sort(a, a + 4,greater<int>());
	int L = 0;
	for (int i = 0; i < 4; i++)
		L += pow(10, 3 - i) * a[i];
	sort(a, a + 4);
	int S = 0;
	for (int i = 0; i < 4; i++)
		S += pow(10, 3 - i) * a[i];
 	string str_newNum = to_string(L - S);
	int length = str_newNum.length();
	for (int i = 0; i < 4 - length; i++)
		str_newNum = '0' + str_newNum;
	return 1 + Func(str_newNum);
}

int main() 
{
	string str_num;
	cin >> str_num;
	while (str_num != "0000")
	{
		int num = stoi(str_num);
		if (num % 1111 == 0)
			cout << "NA\n";
		else
		    cout << Func(str_num) << endl;
		cin >> str_num;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3412
final_rank: 18

code_snippet: #include <bits/stdc++.h>
typedef long long lint;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

int f(string n) {
    int cnt = 0;
    while (n != "6174") {
        sort(n.begin(),n.end());
        int l = stoi(n);
        sort(n.begin(),n.end(), greater<int>());
        int s = stoi(n);
        if (s == l) {
            return -1;
        }
        ostringstream str;
        str << setfill('0') << setw(4) << s-l;
        n = str.str();
        cnt ++;
    }
    return cnt;
}

bool solve() {
    string n; cin >> n;
    if (n == "0000") return false;
    int cnt = f(n);
    if (cnt == -1) cout << "NA" << endl;
    else cout << cnt << endl;

    return true;
}

int main() {
    while (solve()) {}
}
accuracy: 1
time_ms: 20
memory_kb: 3188
final_rank: 16