南阳57

 1 #include<stdio.h>
 2 int a[4];
 3 int main()
 4 {
 5     int n,x,t,i,j,k,maxn,minn;
 6     while(~scanf("%d",&n))
 7     {
 8         while(n--)
 9         {
10             scanf("%d",&x);
11             t=1;
12             while(x!=6174)
13             {
14                 for(i=0,k=1000; i<4; ++i,k/=10)
15                     a[i]=x/k%10;
16                 for(i=0; i<3; ++i)
17                     for(j=0; j<3-i; ++j)
18                         if(a[j]<a[j+1])
19                         {
20                             k=a[j];
21                             a[j]=a[j+1];
22                             a[j+1]=k;
23                         }
24                 for(i=maxn=minn=0,k=1000,j=1; i<4; ++i,k/=10,j*=10)
25                 {
26                     maxn+=a[i]*k;
27                     minn+=a[i]*j;
28                 }
29                 x=maxn-minn;
30                 ++t;
31             }
32             printf("%d\n",t);
33         }
34     }
35 }

 

posted @ 2016-11-23 18:56  Posase  阅读(171)  评论(0编辑  收藏  举报