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 (.

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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 200 ms
内存限制: 64 MB
代码长度限制: 16 KB

题意:

给一个数组n,先对各个数字位从大到小排序得到a,然后逆置得到b,计算a-b,得到c,重复上述过程,直到得到6174,或者,对于特例,四个数字位完全相同,那么输出0000。

题解:

刚开始输入的数字可能不足四位要自动补0 以后的结果可能也不足四位都要自动补0

结果是0或者6174都是要退出的

一开始没考虑到6174,测试点5没过,后来想到了

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[];
int main(){
cin>>n;
int big=;
int small=;
int x=n;
int k=,f;
if(x==){
printf("7641 - 1467 = 6174");
return ;
}
while(x!=){
k=;
big=;
small=;
while(x>=){
a[++k]=x%;
x/=;
}
a[++k]=x;
while(k<){
a[++k]=;
}
sort(a+,a++);
for(int i=;i<=;i++){
big=big*+a[-i];
small=small*+a[i];
}
x=big-small;
printf("%04d - %04d = %04d\n",big,small,x);
if(x==) break;
}
return ;
}

更简洁的string处理的代码:

#include<bits/stdc++.h>
using namespace std;
bool cmp(char a,char b)
{
return a>b;
}
int main(void)
{
string s;
cin>>s;
s.insert(,-s.size(),'');
do{
string a=s,b=s;
sort(a.begin(),a.end(),cmp);
sort(b.begin(),b.end());
int diff=stoi(a)-stoi(b);
s=to_string(diff);
s.insert(,-s.size(),'');
cout<<a<<" - "<<b<<" = "<<s<<endl;
}while(s!=""&&s!="");
return ; ————————————————
版权声明:本文为CSDN博主「Imagirl1」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Imagirl1/article/details/82261213

PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)的更多相关文章

  1. 1069 The Black Hole of Numbers (20分)

    1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...

  2. 【PAT甲级】1069 The Black Hole of Numbers (20 分)

    题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...

  3. 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 ...

  4. 【PAT甲级】1023 Have Fun with Numbers (20 分)

    题意: 输入一个不超过20位的正整数,问乘2以后是否和之前的数组排列相同(数字种类和出现的个数不变),输出Yes或No,并输出乘2后的数字. AAAAAccepted code: #define HA ...

  5. 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 ...

  6. PAT甲级:1124 Raffle for Weibo Followers (20分)

    PAT甲级:1124 Raffle for Weibo Followers (20分) 题干 John got a full mark on PAT. He was so happy that he ...

  7. 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 ...

  8. PAT (Advanced Level) 1069. The Black Hole of Numbers (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  9. PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. redis 缓存问题,转载:https://www.cnblogs.com/liangsonghua/p/www_liangsonghua_me_22.html

    缓存穿透: 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且处于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义 ...

  2. Tomcat8服务

    Windows部署Tomcat8服务在windows上部署Tomcat服务后,可以将Tomcat设为开机启动,即开机后Tomcat就会自动运行.这样就不用每次进到Tomcat的bin目录双击start ...

  3. 移动端videojs视频插件使用直播流rtmp、hls、http-flv的注意事项

    可以访问:https://videojs.com/ 下载对应的脚本包 特别注意的是 移动端videojs一般应用的直播流协议为HLS, RTMP协议一般是PC上使用,需要flash支持. HLS直播源 ...

  4. Zookeeper选举(fastleaderelection算法)

    1.选举相关概念: 选票:(myid,zxid,当前节点选取轮次,被推举服务器选举轮次,状态(looking)). 选举发生情况:启动时选举,运行时选举. 外部投票:其他服务器发送来的投票. 内部投票 ...

  5. 模拟赛 提米树 题解 (DP+思维)

    题意: 有一棵棵提米树,满足这样的性质: 每个点上长了一定数量的Temmie 薄片,薄片数量记为这个点的权值,这些点被标记为 1 到 n 的整数,其 中 1 号点是树的根,没有孩子的点是树上的叶子. ...

  6. java.util.Properties 读取配置文件中的参数

    用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...

  7. 性能测试解读:Kyligence vs Spark SQL

    全球各种大数据技术涌现的今天,为了充分利用大量数据获得竞争优势,企业需要高性能的数据分析平台,可靠并及时地提供对海量数据的分析见解.对于数据驱动型企业,在海量数据上交互式分析的能力是非常重要的能力之一 ...

  8. RegularExpression正则表达式

    1.必須為大寫字母,小寫字母,數字和符號的至少其中3種組合 The new password should contain any three of different syntax which is ...

  9. learning java Math类

    output: //取整,返回小于目标数的最大整数System.out.println(Math.floor(-1.2));// 取整,返回在于目标数的最大整数System.out.println(M ...

  10. http://www.tldp.org/LDP/abs/abs-guide.txt.gz

    http://www.tldp.org/LDP/abs/abs-guide.txt.gz