PAT_A1069#The Black Hole of Numbers
Source:
Description:
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 (.
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 until6174
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
Keys:
- 字符串处理
Attention:
- 早期的PAT考试更注重算法的效率(过于依赖容器会超时),而现在的PAT考试更注重解决问题的能力(强调容器的使用)
- to_string()会超时
Code:
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n1,n2,n;
vector<int> p();
scanf("%d",&n);
do
{
for(int i=; i<; i++)
{
p[i]=n%;
n/=;
}
sort(p.begin(),p.end(),less<char>());
n1 = p[]*+p[]*+p[]*+p[];
sort(p.begin(),p.end(),greater<char>());
n2 = p[]*+p[]*+p[]*+p[];
n = n2-n1;
printf("%04d - %04d = %04d\n", n2,n1,n);
}while(n!= && n!=); return ;
}
PAT_A1069#The Black Hole of Numbers的更多相关文章
- 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 ...
- 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 ...
- pat1069. The Black Hole of Numbers (20)
1069. The Black Hole of Numbers (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...
- 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 分)
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 (20 分)(内含别人string处理的精简代码)
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), ...
- 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 ...
- The Black Hole of Numbers (strtoint+inttostr+sort)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
随机推荐
- C#面试 笔试题 五
1.什么是受管制的代码? 答:unsafe:非托管代码.不经过CLR运行. 2.net Remoting 的工作原理是什么? 答:服务器端向客户端发送一个进程编号,一个程序域编号,以确定对象的位置. ...
- 【学习总结】Eclipse常用快捷键
相关博文 [JAVA]eclipse-Introduction
- Python内置函数(19)-slice
官方文档 class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set o ...
- shell判断/bin目录下date文件是否存在
- 八核浮点型DSP的双千兆网接口设计方案
千兆网络接口具有数据传输速率快.连接方便.可以即插即用的优点,使得其应用较为广泛.随着电子技术和处理器的发展,很多应用场合的数据通信速率超过千兆网口的实际传输速率.例如,在A/D采样中,需要直接存储A ...
- K8S创建的相关yaml文件
一.K8S-yaml的使用及命令 YAML配置文件管理对象 对象管理: # 创建deployment资源 kubectl create -f nginx-deployment.yaml # 查看dep ...
- [IOI1998]Polygon(区间dp)
[IOI1998]Polygon 题意翻译 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条 ...
- redis还要做
RedisTemplate对各种数据类型的操作记录. Redis深度历险:核心原理和应用实践 https://www.cnblogs.com/kismetv/p/8654978.html
- 前后端分离下的CAS跨域流程分析
写在最前 前后端分离其实有两类: 开发阶段使用dev-server,生产阶段是打包成静态文件整个放入后端项目中. 开发阶段使用dev-server,生产阶段是打包成静态文件放入单独的静态资源服务器中, ...
- 最新Zip压缩文件漏洞,黑客可以触发目录遍历攻击
近日,国内某安全公司研究人员透露了一个关键漏洞的详细信息,该漏洞影响了许多生态系统中的数千个项目,黑客可以利用这些漏洞在目标系统上实现代码执行. 黑客是如何通过Zip压缩文件入侵攻击?被称为“ZipS ...