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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 理解依赖注入(IOC)和学习Unity
资料1: IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection). 作用:将各层的对象以松耦合的方式组织在一 ...
- c++11的for新用法 (重新练习一下for_each)
看到手册的代码里面有个for的很奇怪的用法,用了一把 http://www.cplusplus.com/reference/unordered_set/unordered_set/insert/ ...
- qt 5 基础知识 2(控件篇)
QVBoxLayout *lay = new QVBoxLayout(this); // 创建一个竖直的盒子 lebel 篇 lay->addWidget(label = new QLabel( ...
- gulp解决RequireJS
gulp解决RequireJS项目前端缓存问题(二) 前言 这一节,我们主要解决在上一节<使用gulp解决RequireJSs项目前端缓存问题(一)>末尾提到的几个问题: 对通过req ...
- githubRepository -- 使用
1. 注册github账号; 2. 配置SSH keys; 点击setting, 配置 SSH keys Generating SSH keys 检查本地的 SSH keys: a> 在用户目录 ...
- Socket实现简单的聊天通信
最近学习了Socket后,感觉Socket挺好玩的,在博客中看到socket在实时聊天功能的很强大,于是乎就做了一个简单的聊天功能,今天贴出来,能够与大家一起共享,有不对之处,能够给予指出,谢谢! 服 ...
- 关于Matlab作图的若干问题
看到了北京一则新闻,想到如何测试双向镜子?百度之. 只要做以下简单的测试:把你的指甲尖放在镜子表面,如果在指甲尖与倒映图像之间有间隙,那就是真的镜子.然而,如果你 ...
- EasyUI 树形菜单tree 定义图标
{ "id":1, "text":"Folder1", "iconCls":"icon-save", ...
- [笔记] 走进 Pocket,看看只有 20 位员工的 Pocket 是如何搞定 2000 万用户的
走进 Pocket,看看只有 20 位员工的 Pocket 是如何搞定 2000 万用户的 保持专注. 不断更新优先级. 对产品有主人翁意识. (觉得做好产品是自己的职责, 不是应付工作) 你的用户如 ...
- 一个优秀的http实现框架
package com.ming; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unir ...