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 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位数字相同,打印并退出处理)
解题思路
- 用字符串接收整数s,并用0左边补齐整数到4位
- a=s,b=s,a降序排列后转为数字an,b升序排列转为数字bn
- bn-an即为下次处理的整数
易错点
- 若输入的数字为6174,需要打印7641 - 1467 = 6174(否则测试点5错误)(建议使用do...while可以省去针对开始输入为6174的单独处理)
知识点
- 字符串中字符排序
sort(s.begin(),s.end(),cmp); - 利用字符串操作对齐整数
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) [数学问题-简单数学]的更多相关文章
- 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)【模拟】——PAT (Advanced Level) Practise
题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit inte ...
- 1069 The Black Hole of Numbers (20分)
1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...
- 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)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【PAT甲级】1069 The Black Hole of Numbers (20 分)
题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...
- 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 ...
- PAT (Advanced Level) 1023. Have Fun with Numbers (20)
手动模拟一下高精度加法. #include<iostream> #include<cstring> #include<cmath> #include<algo ...
随机推荐
- Web.config中executionTimeout的单位
executionTimeout:表示允许执行请求的最大时间限制,单位为秒
- Flink 复杂事物处理
简介 FlinkCEP是在Flink之上实现的复杂事件处理(CEP)库. 它允许你在无界的事件流中检测事件模式,让你有机会掌握数据中重要的事项. Flink CEP 首先需要用户创建定义一个个patt ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation
AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...
- C# Stream篇(二) -- TextReader 和StreamReader
TextReader 和StreamReader 目录: 为什么要介绍 TextReader? TextReader的常用属性和方法 TextReader 示例 从StreamReader想到多态 简 ...
- python基础(一)内置类型及方法
python 内置类型主要包含7大类: 数字 序列 映射 文件 类 实例 异常 其中,最常用数据类型为序列 序列 python中包含7种内建序列 列表(list):由方括号构成,用逗号分隔项目: [a ...
- QT多线程之---moveToThread用法
在gui编程里,一个子函数的运行时间可能过长,界面就处于假死状态,原因是窗口是一个线程,子函数也在这个线程里,一些事件也要在这个线程里处理. 如果子函数运行时间过长,系统没有办法调用事件监听循环,gu ...
- Git - 版本管理 - 版本回退
1 在历史里找到 SHA-1 的值 0c6ab03dbbfe61e39af92dfe5450bf693a72b7d9 2 命令行里执行:git reset --hard 0c6ab03dbbfe61e ...
- 【pwnable.kr】bof
pwnable从入门到放弃,第三题. Download : http://pwnable.kr/bin/bofDownload : http://pwnable.kr/bin/bof.c Runnin ...
- 15 ~ express ~ 用户数据分页原理和实现
一,在后台路由 /router/admin.js 中 1,限制获取的数据条数 : User.find().limit(Number) 2,忽略数据的前(Number)条数据 : skip(Number ...
- 使用ansible tasks生成linux巡检报告
一直想做个关于资源巡检的功能,其需求就是通过邮件的形式来查看linux资源的使用情况,超出一定的阈值时高亮显示出来.也有人说啦,这个需求通过监控zabbix, prometheus都能做呀,何必自己重 ...