东华大学 2022 oj c++ 无超纲写法 数字黑洞(建议学完函数再回头写4.2)

//写在前面:本人是做完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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值