分析:
以后排序直接用库函数提供的sort很方便。
核心问题在于需要把一个数拆开排序。
其实在判断的时候直接判断6174即可,不需要判断是否和前一个数字相等。
心得:
水题需要锻炼自己的速度。
代码实现:
#include<iostream>
#include<algorithm>
using namespace std;
void ReverseMinus(int &a0)
{
int temp1,temp2;
int *t = new int [4];
t[0] = a0/1000;
t[1] = a0/100%10;
t[2] = a0/10%10;
t[3] = a0%10;
//BubbleSort(t,4);
sort(t,t+4);
temp1 = t[0]+t[1]*10+t[2]*100+t[3]*1000;
temp2 = t[3]+t[2]*10+t[1]*100+t[0]*1000;
a0 = temp1 -temp2;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int a;
cin>>a;
int i=0;
while(++i)
{
int a0= a;
ReverseMinus(a);
if(a0 == a) //和前一个数不相等
{
cout<<i<<endl;
break;
}
}
}
return 0;
}
/*
void BubbleSort(int *a, int n) //冒泡排序
{
int i,j,last;
i = n-1; //n-1趟
while(i>0)
{
last = 0;
for( j=0;j<i;j++)
if(a[j+1]<a[j])
{
int temp;
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
last =j;
}
i = last;
}
}*/