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:

  1. 7766 - 6677 = 1089
  2. 9810 - 0189 = 9621
  3. 9621 - 1269 = 8352
  4. 8532 - 2358 = 6174
  5. 7641 - 1467 = 6174
  6. ... ...

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:

  1. 6767

Sample Output 1:

  1. 7766 - 6677 = 1089
  2. 9810 - 0189 = 9621
  3. 9621 - 1269 = 8352
  4. 8532 - 2358 = 6174

Sample Input 2:

  1. 2222

Sample Output 2:

  1. 2222 - 2222 = 0000
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. bool cmp(int a,int b){
  6. return a> b;
  7. }
  8.  
  9. void to_array(int num[],int n){
  10. for(int i = ; i < ; i++){
  11. num[i] = n % ;
  12. n /= ;
  13. }
  14. }
  15.  
  16. int to_number(int num[]){
  17. int sum = ;
  18. for(int i = ; i < ; i++){
  19. sum = sum * + num[i];
  20. }
  21. return sum;
  22. }
  23.  
  24. int main(){
  25. int n;
  26. scanf("%d",&n);
  27. int num[];
  28. while(){
  29. to_array(num,n);
  30. sort(num,num+);
  31. int min = to_number(num);
  32. sort(num,num+,cmp);
  33. int max = to_number(num);
  34. n = max - min;
  35. printf("%04d - %04d = %04d\n",max,min,n);
  36. if(n == || n == ) break;
  37. }
  38. return ;
  39. }

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. DataSource配置

    一.JDBC Jar依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  2. 模板模式(Template Pattern)

    模板模式(Template Pattern) -- 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. ...

  3. 启动 kibana 失败

    错误信息: Sending Logstash logs to /usr/local/logstash-6.4.3/logs which is now configured via log4j2.pro ...

  4. python之简单爬取一个网站信息

    requests库是一个简介且简单的处理HTTP请求的第三方库 get()是获取网页最常用的方式,其基本使用方式如下 使用requests库获取HTML页面并将其转换成字符串后,需要进一步解析HTML ...

  5. md5 helper

    public static string ToMD5Hash(this string str) { if (string.IsNullOrEmpty(str)) return null; return ...

  6. SAP-参数(条件表)配置教程–GS01/GS02/GS03

    转载:http://www.baidusap.com/abap/others/2849 在SAP开发中,某段代码运行可能需要满足某个条件,通常解决办法有两种:一种是在代码中写死限制条件,此种方式当限制 ...

  7. Apache老版本下载地址

    有时候我们想做测试或者使用旧版本软件时,往往发现官方网站已经下架,以Apache为例: Apache旧版本下载地址:http://archive.apache.org/dist/httpd/ .... ...

  8. javascript_14-对象

    什么是对象 生活中的对象,一个车.一个手机 对象具有特性和行为 面向对象和基于对象 面向对象:可以创建自定义的类型.很好的支持继承和多态.面向对象的语言有 c++ .Java. C# ... 面向对象 ...

  9. [#Linux] CentOS 7 美化调优

    优化美化系统,是为了让新系统能更顺眼顺手,符合自己过去在windows下的使用习惯,从而实现平稳过渡. 正如开篇时谈到的,现在的桌面版linux已相当友好(特别是Ubuntu),基本不需要做什么额外设 ...

  10. 【DRF框架】利用序列化组件操作

    使用序列化组件进行操作 不带参数:查+增 带参数:查.改.删 不带参数的操作 # url路由 url(r'^book_list/$',Search_book.as_view()), # views.p ...