For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 -- the black hole of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we'll get:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...

Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range (.

Output Specification:

If all the 4 digits of N are the same, print in one line the equation N - N = 0000. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

Sample Input 1:

6767

Sample Output 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

Sample Input 2:

2222

Sample Output 2:

2222 - 2222 = 0000

注意点:
(1)题目并未保证输入的n一定是在[1000,10000)之间的数,所以对于输入为53这样的数,第一行输出应为5300 - 0035 = 5265

(2)当输入的数为0或者6174时要特殊判断,输入0输出应为0000 - 0000 = 0000;输入6174输出应为7641 - 1467= 6174,而不能没有输出

(3)输出的数必须为4位,不够4位要在高位补0


 #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int A, B, preAns = -, Ans = , flag = ;
string Na, Nb;
cin >> Ans;
while (preAns != Ans)
{
preAns = Ans;
Na = to_string(Ans);
while (Na.length() < )
Na += "";
sort(Na.begin(), Na.end(), [](char a, char b) {return a > b; });
A = stoi(Na.c_str());
sort(Na.begin(), Na.end(), [](char a, char b) {return a < b; });
B = stoi(Na.c_str());
Ans = A - B;
if (flag == && preAns == Ans)
break;
printf("%04d - %04d = %04d\n", A, B, Ans);
flag = ;//怎么都得有一行输出
}
return ;
}

APT甲级——A1069 The Black Hole of Numbers的更多相关文章

  1. PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)

    1069 The Black Hole of Numbers (20 分)   For any 4-digit integer except the ones with all the digits ...

  2. A1069. The Black Hole of Numbers

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  3. PAT_A1069#The Black Hole of Numbers

    Source: PAT A1069 The Black Hole of Numbers (20 分) Description: For any 4-digit integer except the o ...

  4. PAT 1069 The Black Hole of Numbers

    1069 The Black Hole of Numbers (20 分)   For any 4-digit integer except the ones with all the digits ...

  5. PAT 1069 The Black Hole of Numbers[简单]

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...

  6. pat1069. The Black Hole of Numbers (20)

    1069. The Black Hole of Numbers (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...

  7. 1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise

    题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit inte ...

  8. pat 1069 The Black Hole of Numbers(20 分)

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...

  9. 1069 The Black Hole of Numbers (20分)

    1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...

随机推荐

  1. NPM 使用介绍(包管理工具,解决NodeJS代码部署上的很多问题)

    引用地址:http://www.runoob.com/nodejs/nodejs-npm.html NPM 使用介绍 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问 ...

  2. Entityframwork.extended 配置mysql

    entityframework.extended 这个是个很好的扩展,不过由于默认是配置成MSSQL的,今天在github上面 看到一个解决方案,亲测可用,下面贴代码 1.在DbContext 修改默 ...

  3. 【Vue】组件的基础与组件间通信

    转载:https://segmentfault.com/a/1190000016409329 Vue.js 最核心的功能就是组件(Component),从组件的构建.注册到组件间通信,Vue .x 提 ...

  4. 简单总结Class.forName("").newinstance()和new()以及classLoader.loadClass("")的区别

    文章目录 背景 三种方法简单介绍 Class.forName("").newinstance()方式 new方式 classLoader.loadClass("" ...

  5. day 67 Django基础三之视图函数

    Django基础三之视图函数   本节目录 一 Django的视图函数view 二 CBV和FBV 三 使用Mixin 四 给视图加装饰器 五 Request对象 六 Response对象 一 Dja ...

  6. requests 返回 521

    网页端抓数据免不了要跟JavaScript打交道,尤其是JS代码有混淆,对cookie做了手脚.找到cookie生成的地方要费一点时间. 那天碰到这样一个网页,用浏览器打开很正常.然而用request ...

  7. MT Call来电流程分析????

  8. python基础语法(数据类型转换)

  9. 统计学习笔记之k近邻法

    1.kNN算法的思想:给定一个训练数据集,对新的输入实例,在训练集中找到与该实例最近邻的k个实例,这k个实例的多数属于某类,就把输入实例分为这个类. 2.算法 (1)根据给定的距离度量,在训练集T中找 ...

  10. 两个datagrid的数据移动(支持多选)

    1.需求 :点击卸车和撤销按钮可以实现 1和2 之间数据的移动(支持多选) 2. 代码 (这里只写一个撤销的功能) //撤销按钮 function moveOut() { var item = $(' ...