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:

  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
    此题没有什么难度,基本上就是两个可逆的转换:将一串数字(或者一个字符串)转换为一个整数,或者相反,而这两个转换都是很常见的,司空见惯了。对于此题值得注意的是,不要用字符串来处理(用诸如stringatoiitoagcc上好像没有,可以用memsetsprintf代替】),会超时的!!!什么都不说了,按部就班就好了,请看代码:
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. const int blackHole=;
  8. const int digits=;
  9.  
  10. vector<int> int2vec(int n)
  11. {
  12. vector<int> buf(digits,);
  13. for(int i=;i<digits;++i,n/=)
  14. {
  15. buf[i]=n%;
  16. }
  17. return buf;
  18. }
  19.  
  20. int vec2int(vector<int>& vec)
  21. {
  22. int n=;
  23. int radix=;
  24. for(int i=digits-;i>=;--i)
  25. {
  26. n+=radix*vec[i];
  27. radix*=;
  28. }
  29. return n;
  30. }
  31.  
  32. bool beingTheSame(vector<int>& vec)
  33. {
  34. size_t size=vec.size();
  35. for(int i=;i<size;++i)
  36. {
  37. if(vec[]!=vec[i])
  38. return false;
  39. }
  40. return true;
  41. }
  42.  
  43. int repeat(int n)
  44. {
  45. vector<int> vec=int2vec(n);
  46. sort(vec.begin(),vec.end(),greater<int>());
  47. int first=vec2int(vec);
  48. sort(vec.begin(),vec.end());
  49. int second=vec2int(vec);
  50. int difference=first-second;
  51. printf("%.4d - %.4d = %.4d\n",first,second,difference);
  52. return difference;
  53. }
  54. int _tmain(int argc, _TCHAR* argv[])
  55. {
  56. freopen("1069.txt","r",stdin);
  57. int n;
  58. scanf("%d",&n);
  59. vector<int> vec=int2vec(n);
  60. if(beingTheSame(vec))
  61. {
  62. printf("%.4d - %.4d = 0000\n",n,n);
  63. return ;
  64. }
  65. n=repeat(n);
  66. while(blackHole!=n)
  67. {
  68. n=repeat(n);
  69. }
  70. return ;
  71. }

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

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

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

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

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

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

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

  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特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. 参数TFilterPredicate 类型说明

    类型名称:TFilterPredicate 类型定义: type TFilterPredicate = reference to function(const Path: string, const ...

  2. POJ 1459 Power Network 最大流(Edmonds_Karp算法)

    题目链接: http://poj.org/problem?id=1459 因为发电站有多个,所以需要一个超级源点,消费者有多个,需要一个超级汇点,这样超级源点到发电站的权值就是发电站的容量,也就是题目 ...

  3. aspose.cells根据模板导出excel

    又隔十多天没写博客了,最近都在忙项目的事情,公司人事变动也比较大,手头上就又多了一个项目.最近做用aspose.cells根据模板导出excel报价单的功能,顺便把相关的核心记下来,先上模板和导出的效 ...

  4. tableView被Nav挡住了

    // 1. //    self.navigationController.navigationBar.translucent = NO; //    self.tabBarController.ta ...

  5. DM8168 环境搭建(2) ------ 虐心之旅

    续上  ... ... ... (5)安装minicom minicom类似于windows下的超级终端,用于与串口设备通信    参考命令:sudo apt-get install minicom ...

  6. [JavaScript] js 复制到剪切板

    zeroclipboard官网:https://github.com/zeroclipboard/ZeroClipboard 下载压缩包,得到两个“ZeroClipboard.js”和“ZeroCli ...

  7. BZOJ 1592: [Usaco2008 Feb]Making the Grade 路面修整

    Description FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了 ...

  8. Linux Screen命令使用

    参考URL: http://jingyan.baidu.com/article/295430f128d8ea0c7e005089.html ~~~~~~~~~~~~~~~~~~~~~~~~ 其它的不提 ...

  9. 【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)

    [题意] 有n个绿洲, m条道路,每条路上有一个温度,和一个路程长度,从绿洲s到绿洲t,求一条道路的最高温度尽量小, 如果有多条, 选一条总路程最短的. InputInput consists of ...

  10. block的是发送信号的线程,又不是处理槽函数的线程

    请问UI线程给子线程发信号,应该用哪种连接方式? 如果子线程正在执行一个函数,我发射信号去执行子线程的另一个函数,那么此时子线程到底会执行什么呢? 用信号量做的同步.第一把信号槽的事件丢到线程的事件队 ...