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. Git config 使用说明(转)

    原文:https://blog.csdn.net/gdutxiaoxu/article/details/79253737

  2. gulp删除目标文件中所有的console.log()语句——gulp-strip-debug

    1.安装npm包 npm install --save-dev gulp-strip-debug 2.使用 const gulp = require('gulp'); const stripDebug ...

  3. CentOS7.9防火墙命令

    CentOS7防火墙命令有变化: CentOS7:   systemctl status firewalld.service     查看防火墙状态 systemctl stop firewalld. ...

  4. SQL Server存储过程中变量使用函数调用变量

    USE  DB名称GO SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO . CREATE   PROCEDURE     存储过程名 @formID   n ...

  5. Saas软件更新以及小程序更新的教训

    Saas软件即使版本更新多次,也要兼顾老客户,兼容旧功能. 对于小程序调用的接口,无法保证客户会更新小程序,因此需要兼容使用旧版本小程序的客户,更不能删除接口.

  6. HTML标签认识一

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. UI5-技术篇-SEGW DEEP ENTITY测试

    参考:http://blog.sina.com.cn/s/blog_a7d67d810102xgms.html 一般调用后端的OData服务需要同时传输多个记录表对业务逻辑进行处理(例如BAPI),今 ...

  8. Arc Engine二次开发——弹窗进行属性查询

    在Arcmap中使用Sapefile格式的矢量数据时,经常会用到其属性查询的功能,弹出窗体然后用户鼠标点击或手动输入查询条件,进而查询到感兴趣的要素.在AE二次开发中也经常需要这个功能,于是在此记录整 ...

  9. SPI简述

    SPI是器件的比较常用的通信协议. SPI总共有四根线: SS:片选线,每个设备都和主机MCU有一条单独片选线相连,片选线拉低意味主机输出,也就是说一个主机可以和多个从机相连,只需要有足够多的片选线. ...

  10. 原生Ajax代码实现

    Ajax Asynchronous JavaScript And XML   异步: 指一段程序执行时不会阻塞其他程序执行,其表现形式为程序的执行顺序不依赖程序本身的书写顺序 ,相反的则为同步, 自己 ...