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. item 6: 当auto推导出一个不想要的类型时,使用显式类型初始化的语法

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 Item 5解释了比起显式指定类型,使用auto来 ...

  2. xmlSpy套件(Altova MissionKit 2016)的Ollydbg调试过程及破解

    最近工作需要用到XML处理软件,网上找到Altova MissionKit 2016( 包含了XmlSpy.MapForce.StyleVision.UModel.DatabaseSpy等工具),用了 ...

  3. Ceph分布式存储-运维操作笔记

    一.Ceph简单介绍1)OSDs: Ceph的OSD守护进程(OSD)存储数据,处理数据复制,恢复,回填,重新调整,并通过检查其它Ceph OSD守护程序作为一个心跳 向Ceph的监视器报告一些检测信 ...

  4. OSGI的WEB开发环境搭建

    第一步,搭建OSGI环境: 打开eclipse,点击run->run configration..,配置如下,点击run. 运行结果如下图所示:说明OSGI环境搭建完毕. 第二步:搭建基于OSG ...

  5. python基础学习笔记(六)

    学到这里已经很不耐烦了,前面的数据结构什么的看起来都挺好,但还是没法用它们做什么实际的事. 基本语句的更多用法 使用逗号输出 >>> print 'age:',25 age: 25 ...

  6. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-A-Single Wildcard Pattern Matching

    #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> ...

  7. PAT 1038 统计同成绩学生

    https://pintia.cn/problem-sets/994805260223102976/problems/994805284092887040 本题要求读入N名学生的成绩,将获得某一给定分 ...

  8. Activiti reassign task to another user

    //早先胡乱尝试的其他方法,可能对于以后深入学习Activiti有些用处. //taskService.delegateTask(taskId, receiveUserId); //taskServi ...

  9. 转帖--计算机网络基础知识大总汇 https://www.jianshu.com/p/674fb7ec1e2c?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

    计算机网络基础知识大总汇 龙猫小爷 关注 2016.09.14 23:01* 字数 12761 阅读 30639评论 35喜欢 720 一.什么是TCP/IP 网络和协议 1.     TCP/IP是 ...

  10. ORA-01654 : 表空间不足

    参考: Oracle表空间不足ORA-01654 查看表空间和表的使用率 ORA-01654 索引 无法通过 表空间扩展 Oracle 查看表空间的大小及使用情况sql语句 一.基础查询 1.查看表空 ...