当前位置:网站首页>PAT Class B-B1019 Digital Black Hole (20)
PAT Class B-B1019 Digital Black Hole (20)
2022-08-05 09:13:00 【nekoha_dexter】
Given any 4-digit positive integer whose digits are not exactly the same, if we first sort the 4 numbers non-increasingly, then non-decreasingly, and then subtract the 2nd number from the 1st number, we will geta new number.Keep doing this over and over again, and we'll soon stop at 6174 known as the "digital black hole", the magic number also known as the Kaprekar constant.
For example, if we start with 6767, we will get
7766 - 6677 = 10899810 - 0189 = 96219621 - 1269 = 83528532 - 2358 = 61747641 - 1467 = 6174 …Given any 4-digit positive integer, write a program to demonstrate the process of reaching a black hole.
input format:
The input gives a positive integer N in the interval (0,104).
Output format:
If all 4 digits of N are equal, output N - N = 0000 on one line; otherwise, output each step of the calculation on one line until 6174As difference occurs, see example for output format.Note that each number is output as 4 digits.
Input Sample 1:
6767Output Sample 1:
7766 - 6677 = 10899810 - 0189 = 96219621 - 1269 = 83528532 - 2358 = 6174Input Sample 2:
2222Output Sample 2:
2222 - 2222 = 0000#include#includeusing namespace std;string s1,s2;int main(){cin >> s1;int ans;//s1 may be less than 4 digits, 0 should be inserteds1.insert(0,4-s1.size(),'0');do{sort(s1.begin(), s1.end());s2 = s1;//s1 is from big to small at this timereverse(s1.begin(), s1.end());cout < 边栏推荐
猜你喜欢
随机推荐
express hot-reload
selectPage 动态改变参数方法
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
DPU — 功能特性 — 网络系统的硬件卸载
【Excel实战】--图表联动demo_001
【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
【LeetCode】623. Add a row to the binary tree
工程制图试题
Does flink cdc support synchronization from oracle dg library?
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
ts/js 函数传参带函数写法
Excuse me if you want to write data in mysql, with flink - connector - JDBC directly is ok, but I'm in the f
使用HBuilder离线本地打包ipa教程
Xcode 12 ld: symbol(s) not found for architecture armv64
HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
六年团队Leader实战秘诀|程序员最重要的八种软技能 - 脸皮薄容易耽误事 - 自我营销
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
Two-table query average grouping in sql server








