已AC代码:
#include<cstdio>
#include<algorithm>
using namespace std;
bool compare(int x, int y)
{
return x > y;
}
void rever(int n, int a[])
{
int i=0;
while(n)
{
a[i] = n%10;
n /= 10;
i++;
}
}
int reback(int a[])
{
int temp=0, d=1000;
for(int i=0; i<4; i++)
{
temp += a[i] * d;
d /= 10;
}
return temp;
}
int deal(int a[], int n, int cont)
{
int d, c,tend;
sort(a, a+4, compare);
d = reback(a);
sort(a, a+4);
c = reback(a);
tend = d-c;
if(tend != n)
{
cont++;
rever(tend, a);
deal(a, tend, cont);
}
else
return cont;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, a[4];
scanf("%d", &n);
rever(n, a);
printf("%d\n", deal(a, n, 1));
}
return 0;
}