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 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
此题没有什么难度,基本上就是两个可逆的转换:将一串数字(或者一个字符串)转换为一个整数,或者相反,而这两个转换都是很常见的,司空见惯了。对于此题值得注意的是,不要用字符串来处理(用诸如string、atoi,itoa【gcc上好像没有,可以用memset和sprintf代替】),会超时的!!!什么都不说了,按部就班就好了,请看代码:
- #include <cstdio>
- #include <algorithm>
- #include <functional>
- #include <vector>
- using namespace std;
- const int blackHole=;
- const int digits=;
- vector<int> int2vec(int n)
- {
- vector<int> buf(digits,);
- for(int i=;i<digits;++i,n/=)
- {
- buf[i]=n%;
- }
- return buf;
- }
- int vec2int(vector<int>& vec)
- {
- int n=;
- int radix=;
- for(int i=digits-;i>=;--i)
- {
- n+=radix*vec[i];
- radix*=;
- }
- return n;
- }
- bool beingTheSame(vector<int>& vec)
- {
- size_t size=vec.size();
- for(int i=;i<size;++i)
- {
- if(vec[]!=vec[i])
- return false;
- }
- return true;
- }
- int repeat(int n)
- {
- vector<int> vec=int2vec(n);
- sort(vec.begin(),vec.end(),greater<int>());
- int first=vec2int(vec);
- sort(vec.begin(),vec.end());
- int second=vec2int(vec);
- int difference=first-second;
- printf("%.4d - %.4d = %.4d\n",first,second,difference);
- return difference;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- freopen("1069.txt","r",stdin);
- int n;
- scanf("%d",&n);
- vector<int> vec=int2vec(n);
- if(beingTheSame(vec))
- {
- printf("%.4d - %.4d = 0000\n",n,n);
- return ;
- }
- n=repeat(n);
- while(blackHole!=n)
- {
- n=repeat(n);
- }
- return ;
- }
PAT 1069. The Black Hole of Numbers (20)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 1069 The Black Hole of Numbers (20分)
1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...
- 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 ...
- 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 ...
- 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 ...
- PAT (Advanced Level) 1069. The Black Hole of Numbers (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 参数TFilterPredicate 类型说明
类型名称:TFilterPredicate 类型定义: type TFilterPredicate = reference to function(const Path: string, const ...
- POJ 1459 Power Network 最大流(Edmonds_Karp算法)
题目链接: http://poj.org/problem?id=1459 因为发电站有多个,所以需要一个超级源点,消费者有多个,需要一个超级汇点,这样超级源点到发电站的权值就是发电站的容量,也就是题目 ...
- aspose.cells根据模板导出excel
又隔十多天没写博客了,最近都在忙项目的事情,公司人事变动也比较大,手头上就又多了一个项目.最近做用aspose.cells根据模板导出excel报价单的功能,顺便把相关的核心记下来,先上模板和导出的效 ...
- tableView被Nav挡住了
// 1. // self.navigationController.navigationBar.translucent = NO; // self.tabBarController.ta ...
- DM8168 环境搭建(2) ------ 虐心之旅
续上 ... ... ... (5)安装minicom minicom类似于windows下的超级终端,用于与串口设备通信 参考命令:sudo apt-get install minicom ...
- [JavaScript] js 复制到剪切板
zeroclipboard官网:https://github.com/zeroclipboard/ZeroClipboard 下载压缩包,得到两个“ZeroClipboard.js”和“ZeroCli ...
- BZOJ 1592: [Usaco2008 Feb]Making the Grade 路面修整
Description FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了 ...
- Linux Screen命令使用
参考URL: http://jingyan.baidu.com/article/295430f128d8ea0c7e005089.html ~~~~~~~~~~~~~~~~~~~~~~~~ 其它的不提 ...
- 【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)
[题意] 有n个绿洲, m条道路,每条路上有一个温度,和一个路程长度,从绿洲s到绿洲t,求一条道路的最高温度尽量小, 如果有多条, 选一条总路程最短的. InputInput consists of ...
- block的是发送信号的线程,又不是处理槽函数的线程
请问UI线程给子线程发信号,应该用哪种连接方式? 如果子线程正在执行一个函数,我发射信号去执行子线程的另一个函数,那么此时子线程到底会执行什么呢? 用信号量做的同步.第一把信号槽的事件丢到线程的事件队 ...