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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- EncodingUtils 编译不通过
在Android Studio中开发, 将字符数组转成字符串: Strin re= EncodingUtils.getString(bytes,"UTF-8"); 可是提示Enco ...
- View和ViewGroup的区别 -- Touch事件处理
View.java源码: /frameworks/base/core/java/android/view/View.java View.java的 dispatchTouchEvent 方法: 经过一 ...
- python使用sqlite3
import sqlite3 mysqldb=sqlite3.connect(r"C:\Users\Administrator\Desktop\testdb.db") mysqld ...
- copy,retain,assign,strong,weak的区别
引用地址:http://www.aichengxu.com/view/32930 一.assign,copy,retain 1.copy是内容复制,新建一个相同内容的不同指针,retain为指针复制, ...
- 在ios开发中有多少常用的加密解密方式(备用)
最常用的是MD5和base64编码,还有DES 3DES AES加密 ios怎么实现RAS加密解密 最近几天折腾了一下如何在iOS上使用RSA来加密.iOS上并没有直接的RSA加密API.但是iOS提 ...
- CSS标签居中
CSS标签居中是相对于父标签说的,即在父标签的中居中.通常是在子标签中使用margin:0 auto,来使子标签居中.此外子标签需要有固定的宽度才行,比如 子标签为div时,div的宽度默认占父标签的 ...
- linux线程(二)内存释放
linux线程有两种模式joinable和unjoinable. joinable线程:系统会保存线程资源(栈.ID.退出状态等)直到线程退出并且被其他线程join. unjoinable线程:系统会 ...
- 记录一次配置unix网络编程环境的过程和遇到的问题
记录一次搭建unix网络编程环境过程中遇到的问题和总结 计算机环境虚拟机 linuxmint-18-xfce-64bit 1.打开unix网络编程.iso 把目录下的文件复制到某一目录,修改权限,可命 ...
- ECshop 在迁移到 PHP7 时遇到的兼容性问题
在 PHP7 上安装 ECShop V2.7.3时,报错! Deprecated: Methods with the same name as their class will not be cons ...
- IIS tomcat共用80端口解决一个IP多个域名:使用Nginx反向代理方式使两者兼容
环境: windows server 2003,IIS6服务器,Tomcat7服务器 域名有几个: 以下是使用IIS的域名: http://www.formuch.com/ http://www.fo ...