题目

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in nonincreasing 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 diference. 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

题目分析

已知一个最多有4位的整数N,对N降序排列-对N升序排列=下一次的整数N,循环处理直到N=6174为止,打印其处理过程(特殊情况:如果N的4位数字相同,打印并退出处理)

解题思路

  1. 用字符串接收整数s,并用0左边补齐整数到4位
  2. a=s,b=s,a降序排列后转为数字an,b升序排列转为数字bn
  3. bn-an即为下次处理的整数

易错点

  1. 若输入的数字为6174,需要打印7641 - 1467 = 6174(否则测试点5错误)(建议使用do...while可以省去针对开始输入为6174的单独处理)

知识点

  1. 字符串中字符排序

    sort(s.begin(),s.end(),cmp);
  2. 利用字符串操作对齐整数

    2.1 右对齐。如:4位对齐,输入1,要求得到"0001";输入11,要求得到"0011"

    s.insert(0,4-s.length,'0');

    2.2 左对齐。4位对齐,输入1,要求得到"1000",输入11,要求得到"1100"

    s.insert(s.length,4-s.length,'0');

Code

Code 01

#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(char a, char b) {
return a>b;
}
int main(int argc, char * argv[]) {
string s,a,b;
cin>>s;
s.insert(0,4-s.length(),'0');
do {
a=s,b=s;
sort(a.begin(),a.end(),cmp);
sort(b.begin(),b.end());
int res = stoi(a)-stoi(b);
s=to_string(res);
s.insert(0,4-s.length(),'0');
cout<<a<<" - "<<b<<" = "<<s<<endl;
} while(s!="6174"&&s!="0000"); return 0;
}

PAT Advanced 1069 The Black Hole of Numbers (20) [数学问题-简单数学]的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  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. PAT (Advanced Level) 1023. Have Fun with Numbers (20)

    手动模拟一下高精度加法. #include<iostream> #include<cstring> #include<cmath> #include<algo ...

随机推荐

  1. Maven与nexus关系

    一.了解Maven,Maven用来干什么呢 1. 优秀的构建工具 通过简单的命令,能够完成清理.编译.测试.打包.部署等一系列过程.同时,不得不提的是,Maven是跨平台的,无论是在Windows.还 ...

  2. 为什么vue中的data用return返回

    1.为什么在项目中data需要使用return返回数据呢? 不使用return包裹的数据会在项目的全局可见,会造成变量污染:使用return包裹后数据中变量只在当前组件中生效,不会影响其他组件. 当一 ...

  3. 基础语法-循环结构do...while

    基础语法-循环结构do...while 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.do...while语句格式 do{ 执行语句; }while(条件表达式); 温馨提示: ...

  4. 再战希捷:西部数据透露96层闪存已用于消费级SSD

    导读 96层堆叠3D NAND闪存已经成为行业主流,包括西部数据这样的传统机械硬盘大厂,也在逐步普及96层闪存,并已经用于消费级SSD. 96层堆叠3D NAND闪存已经成为行业主流,包括西部数据这样 ...

  5. 【LeetCode】二叉树的最大深度

    [问题]给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数.说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...

  6. ContentProvider ContentResolver ContentObserver 内容:提供、访问、监听

    内容提供 public class PersonContentProvider extends ContentProvider{ private static final String AUTHORI ...

  7. [题解] Luogu P4721 【模板】分治 FFT

    分治FFT的板子为什么要求逆呢 传送门 这个想法有点\(cdq\)啊,就是考虑分治,在算一段区间的时候,我们把他分成两个一样的区间,然后先做左区间的,算完过后把左区间和\(g\)卷积一下,这样就可以算 ...

  8. 吴裕雄--天生自然 JAVASCRIPT开发学习:事件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 2020/2/3 PHP代码审计之PHP伪协议

    0x00 简介 开局一张图233 0x01 file://协议 说明: file:// 文件系统是 PHP 使用的默认封装协议,展现了本地文件系统.当指定了一个相对路径(不以/..\或 Windows ...

  10. 2020/2/3 PHP代码审计之PHP弱类型

    0x00 简介 php中有两种比较的符号 == 与 === <?php 2 $a = $b ; 3 $a===$b ; 4 ?> === 在进行比较的时候,会先判断两种字符串的类型是否相等 ...