Source:

PAT A1069 The Black Hole of Numbers (20 分)

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

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的更多相关文章

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

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

  3. pat1069. The Black Hole of Numbers (20)

    1069. The Black Hole of Numbers (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...

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

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

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

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

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

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

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

随机推荐

  1. json字符串格式

    private static final String COMPLEX_JSON_STR = "{" + "\"teacherName\":\&quo ...

  2. canvas添加事件

    https://blog.csdn.net/xundh/article/details/78722744

  3. Spring Boot 静态资源处理,妙!

    作者:liuxiaopeng https://www.cnblogs.com/paddix/p/8301331.html 做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如 ...

  4. eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误

    eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误 具体描述: 整体编译能够编译成功,但新加一个java,里面创建一个main方法,运行时,报无法加载主类的错误, 整体编 ...

  5. 最短路径(floyd和Dijkstra)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. CodeChef A String Game(SG)

    A String Game   Problem code: ASTRGAME   Submit All Submissions   All submissions for this problem a ...

  7. centos7 nginx完整支持thinkphp pathinfo模式开启方法

    thinkphp运行在linux+nginx,如何开启pathinfo模式,我是完整测试过了,没问题的,在thinkphp配置文件 开启    'URL_MODEL'   =>  1,   1代 ...

  8. CSS 实现水平垂直居中

    使用绝对定位 在已经知道子元素的宽高的时候,子元素设置成绝对定位,top,left,right,bottom=0, margin = auto .wrap{ position: relative; w ...

  9. JVM(8)之 Stop The World

    开发十年,就只剩下这套架构体系了! >>>   小伙伴还记得上一篇中我们留下的一个问题吗?什么是停顿类型!经过前几章的学习,我们知道垃圾回收首先是要经过标记的.对象被标记后就会根据不 ...

  10. 【学习总结】GirlsInAI ML-diary day-20-初识 Kaggle

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day20 初识kaggle 1-注册一个账号(由于被谷歌收购,因此可能需要梯子) 2-Competition - 学会看一 ...