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
#include<cstdio>
#include<algorithm>
using namespace std; bool cmp(int a,int b){
return a> b;
} void to_array(int num[],int n){
for(int i = ; i < ; i++){
num[i] = n % ;
n /= ;
}
} int to_number(int num[]){
int sum = ;
for(int i = ; i < ; i++){
sum = sum * + num[i];
}
return sum;
} int main(){
int n;
scanf("%d",&n);
int num[];
while(){
to_array(num,n);
sort(num,num+);
int min = to_number(num);
sort(num,num+,cmp);
int max = to_number(num);
n = max - min;
printf("%04d - %04d = %04d\n",max,min,n);
if(n == || n == ) break;
}
return ;
}

1069 The Black Hole of Numbers(20 分)的更多相关文章

  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. 1069 The Black Hole of Numbers (20分)

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

  3. 【PAT甲级】1069 The Black Hole of Numbers (20 分)

    题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...

  4. 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 ...

  5. PAT 1069. The Black Hole of Numbers (20)

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

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

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

  7. PAT Advanced 1069 The Black Hole of Numbers (20) [数学问题-简单数学]

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

  8. PAT (Advanced Level) 1069. The Black Hole of Numbers (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  9. PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  10. 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 ...

随机推荐

  1. The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]

    也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...

  2. docker 启动 容器----bootstrap checks failed

    错误信息: bootstrap checks failed 解决方法: 1.修改elasticsearch.yml配置文件,允许外网访问. vim config/elasticsearch.yml,增 ...

  3. C#汉字转换成全拼的拼音

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. C#基础加强笔记

    1面向对象 类:包含字段.属性.函数.构造函数 字段:存储数据 属性:保护字段 get set 函数:描述对象的行为 构造函数:初始化对象,给对象的每个属性赋值 面向对象的好处:让程序具有扩展性 类决 ...

  5. axios 内部原理学习记录

    前提:一次面试被问到了,axios有什么特点,对比一下ajax.答的很不满意. axios是一个基于Promise的http请求库,可用于浏览器和 Node.可以说是目前最为常用的http库,有必要了 ...

  6. Eclipse 交叉编译环境

    创建空工程 添加交叉编译环境 添加工程文件 如需修改交叉编译环境 Cross GCC:使用交叉编译命令编译,需要自己指定 MinGW GCC:使用make命令编译,需要有Makefile Make T ...

  7. jmeter安装,汉化

    下载完成后打开bin文件,选择jmeter.properties打开,搜索language,修改成zh_CN,汉化jmeter,记得去掉前面的#号,然后保存,修改完配置文件后需要重启jmeter 用的 ...

  8. Python中的上下文管理器(contextlib模块)

    上下文管理器的任务是:代码块执行前准备,代码块执行后收拾 1 如何使用上下文管理器: 打开一个文件,并写入"hello world" filename="my.txt&q ...

  9. Django modle基础样版

    定义一个基类模版, from django.db import models class ModelBase(models.Model): """ "" ...

  10. Zookeeper配置安装

    单机模式: 1:修改hosts文件 ​ 2:下载解压zookeeper.tar文件 3:创建data目录和logs目录 进入zookeeper目录下创建 ​ 4:复制一份zoo_sample.cfg配 ...