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 (0, 10000).

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<iostream>
#include<algorithm>
using namespace std;
bool cmp1(int a, int b){
return a < b;
}
bool cmp2(int a, int b){
return a > b;
}
void numSort(int n, int &r1, int &r2){
int temp[];
int i = ;
r1 = ; r2 = ;
do{
temp[i++] = n % ;
n = n / ;
}while(n != || i < );
sort(temp, temp + i, cmp1);
for(int j = , P = ; j < i; j++){
r1 = r1 + P * temp[j];
P = P * ;
}
sort(temp, temp + i, cmp2);
for(int j = , P = ; j < i; j++){
r2 = r2 + P * temp[j];
P = P * ;
}
}
int main(){
int N, r1, r2, ans;
scanf("%d", &N);
numSort(N, r1, r2);
do{
ans = r1 - r2;
printf("%04d - %04d = %04d\n", r1, r2, ans);
numSort(ans, r1, r2);
}while(ans != && ans != );
cin >> N;
return ;
}

总结:

1、注意在int转换为num[ ]数组时,如果不够四位,应补全成四位,否则答案会出错。(15应转换为0015和1500,而不是15和50)。

A1069. The Black Hole of Numbers的更多相关文章

  1. APT甲级——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 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. React之父子组件传递和其它一些要点

    react是R系技术栈中最基础同时也是最核心的一环,2年不到获取了62.5k star(截止到目前),足可见其给力程度.下面对一些react日常开发中的注意事项进行罗列. React的组件生命周期 r ...

  2. Centos7.3下安装Jumpserver 1.0.0(支持windows组件)

    Jumpserver最新版本支持windows组件,废话不多介绍了,下面直接介绍下部署过程: 0)系统环境 CentOS 7.3 IP: 192.168.10.210 [root@jumpserver ...

  3. Mysql基于GTID复制模式-运维小结 (完整篇)

    先来看mysql5.6主从同步操作时遇到的一个报错:mysql> change master to master_host='192.168.10.59',master_user='repli' ...

  4. Python-注册登陆-20

    username = input('请输入你要注册的用户名:') password = input('请输入你要注册的密码:') with open('list_of_info',mode='w',e ...

  5. jenkins 上命令各种找不到问题

    代码: 兵马未动,粮草先行 作者: 传说中的汽水枪 如有错误,请留言指正,欢迎一起探讨. 转载请注明出处.   在使用jenkins的时候,会使用一些命令行,有的时候明明在电脑的命令行(终端)可以执行 ...

  6. 作业20171130 final发布 成绩

    申诉 对成绩有疑问或不同意见的同学,请在群里[@杨贵福]. 申诉时间截止2017年12月16日 17:00. 更新 第一周和第二周成绩分别应占比20%和10%,计算时刚好反了.所以同学们的最终成绩有变 ...

  7. What is 软件工程

    话说新的一学期,有一门叫软件工程的专业课,作为计算机科学与技术专业的学生,在上课前有几个问题 1.软件工程顾名思义是学软件,可是软件这个词范围还是挺大的,那到底学的是哪方面,是编程?设计APP?还是一 ...

  8. IO学习

    ---恢复内容开始--- 命名规则: 1.类名使用 UpperCamelCase 风格,必须遵从驼峰形式,但以下情形例外:(领域模型 的相关命名)DO / BO / DTO / VO 等. 2.方法名 ...

  9. JavaScript获取DOM节点

    常用的方法有 document.getElementById("id"); document.getElementsByTagName('tagName'); document.g ...

  10. Enterprise Integration Patterns

    https://camel.apache.org/enterprise-integration-patterns.html 企业集成模式,各种模式算法,挺棒的. https://camel.apach ...