//写在前面:本人是做完7.1 7.2(函数)之后再回头做前面卡了很久的3.2 4.2 5.2的,正如函数一章的取名:过程封装,会让整个解题过程变得模块化,有序,
//来看我写法的dhu同学们也可以参考一下哦
//初学,如有不对,欢迎讨论,指正
AC代码:
#include<iostream>
#include<iomanip>
#include<math.h>
#include<cstring>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<bits/stdc++.h>
int reverse(int x)
{
int y = 0;
while (x)
{
y = 10 * y + x % 10;
x /= 10;
}
return(y);
};//设计一个函数满足题目的第二步
int maxnum(int p)
{
char num[4];
num[0] = p/1000;
num[1] = p % 1000 / 100;
num[2] = p % 100 / 10;
num[3] = p % 10;
for (int i = 1; i < 4; i++)
{
for (int j = 0; j < 4 - i; j++)
{
if(num[j]>num[j+1])
{
int temp = num[j];
num[j] = num[j+1];
num[j + 1] = temp;
}
}
}
int max = 0;
max = 1000 * num[3] + 100 * num[2] + 10 * num[1] + num[0];
return(max);
};//设计一个函数满足题目的第一步
using namespace std;
int main()
{
int a;
cin >> a;
int cnt = 0;
while (a != 6174)
{
a = maxnum(a) - reverse(maxnum(a));
cnt++;
}
cout << cnt;
//system("pause");
return 0;
}