题目如下:

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

题目要求将一个四位数按照降序、升序生成减数和被减数然后作差,如果差等于0或者6174则停止运算。

题目的注意点在于数字不够4位时要前补0。

为了方便的排序四位数,我们先使用vector容纳四位然后排序,接着利用stringstream将vecotr中元素转成数字,因为输出时减数、被减数和差都需要,因此用一个结构体存储每次的运算结果。

代码如下:

#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <sstream>
#include <stdio.h>using namespace std;struct Result{int num1;int num2;int result;
}res;void compute(int input){vector<int> num1,num2;num1.push_back(input / 1000);num1.push_back(input % 1000 / 100);num1.push_back(input % 100 / 10);num1.push_back(input % 10);num2 = num1;sort(num1.begin(),num1.end(),less_equal<int>());sort(num2.begin(),num2.end(),greater_equal<int>());stringstream ss;for(int i = 0; i < 4; i++){ss << num1[i];}int dec1;ss >> dec1;ss.clear();for(int i = 0; i < 4; i++){ss << num2[i];}int dec2;ss >> dec2;res.num1 = dec2;res.num2 = dec1;res.result = dec2 - dec1;
}int main()
{int input;cin >> input;int i = 5;do{compute(input);input = res.result;if(input == 0){printf("%04d - %04d = %04d\n",res.num1,res.num2,input);break;}else{printf("%04d - %04d = %04d\n",res.num1,res.num2,input);}}while(input != 6174);return 0;
}

1069. The Black Hole of Numbers (20)相关推荐

  1. 【PAT甲级 前导0,排序】1069 The Black Hole of Numbers (20 分) C++ 全部AC

    题目 一直循环相减即可,直到结果为6174或者四个数字相同就结束循环 题解 C++ #include<iostream> #include<stdio.h> #include& ...

  2. 15行代码AC——1019 数字黑洞 (20分) 甲级1069. The Black Hole of Numbers (20)(解题报告)

    立志用更少的代码做更高效的表达 PAT甲级最优题解-->传送门 Pat乙级最优化代码+题解+分析汇总-->传送门 给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非 ...

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

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

  4. PAT甲级1069 The Black Hole of Numbers:[C++题解]模拟、6174

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 注意特判 6174这个数:if(n == 6174) printf("7641 - 1467 = 6174\n" ...

  5. [PAT-A 1069]The Black Hole of Numbers

    题目大意: 给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字. 已知这样做,答案就会停在61 ...

  6. 1069 The Black Hole of Numbers

    注意两点: 1. 不足4位要补足,不仅仅是一开始要考虑,每次得到一个差值,都要考虑 2. 到0也会停下,不仅仅是一开始可能发生,也可能是过程中的某一个差值 另: vector<int> 是 ...

  7. PTA甲级考试真题练习69——1069 The Black Hole of Numbers

    题目 思路 水题,利用sprintf和sscanf进行字符串和整数的格式转换 代码 #include <iostream> #include<algorithm> using ...

  8. pat-1069 The Black Hole of Numbers (20分)

    感觉我的解答比很多题解都好,嘻嘻就是模板的使用 但是需要去做注意一个测试点,如果输出的数字是一个小于1000的数,就是数位小于4,那么就需要自己去使用string的匿名函数去补上前导0 代码如下: # ...

  9. 【PAT (Advanced Level) Practice】1120 Friend Numbers (20 分)

    1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...

最新文章

  1. Fragment提交transaction导致state loss异常
  2. 做人、做事,做架构师——架构师能力模型解析
  3. r语言线性回归_(R语言)线性回归:机器学习基础技术
  4. mysql主键查询gap锁失效,mysql记录锁(record lock),间隙锁(gap lock),Next-key锁(Next-key lock)...
  5. dojo 加载自定义module的路径问题
  6. IDC商人应不应该给客户提供服务器测试?```
  7. MyBatis学习 之 七、mybatis各种数据库的批量修改
  8. 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(1月9日-1月15日)
  9. 创业者请不断自问:我能帮用户解决什么问题?
  10. win7使用命令行改计算机名,Win7巧用注册表更改计算机名的实用方法
  11. PLSQL Developer新手使用教程(图文教程)
  12. 量子计算机采用量子力学原理,量子力学原理及其应用.docx
  13. 用74HC595和74LS247驱动四位数码管
  14. 谷歌发布最强AI机器人AlphaGo Zero,融360拟融资3亿美元即将赴美上市 | 大数据周周看
  15. “作为女程序员,我成了国内唯一的 GitHub Star”
  16. hadoop-ykt(自定义key)
  17. NASA12.5米高程DEM下载与5米等高线的提取方法
  18. Ubuntu 14.04 Linux 3D桌面完全教程,显卡驱动安装方法,compiz特效介绍,常见问题解答
  19. 分众传媒的数字化厮杀继续
  20. 痞子衡嵌入式:在IAR开发环境下将整个源文件代码重定向到任意RAM中的方法

热门文章

  1. 【DT】蒸脱机的结构和工作原理
  2. Android9.0 Wifi模块Framework层分析
  3. 官方文档翻译《The Libra Blockchain》之执行交易(二)
  4. 哪些 iOS 应用让你用了很满意并能提高生活质量?
  5. 慧算账V2.0版发布,互联网记账再升级
  6. 安卓低功耗蓝牙——手机作为外围设备
  7. MATLAB学习笔记:行列式及其应用
  8. google AdSense广告不显示的原因
  9. brpc线程模型学习
  10. 攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)