当前位置:网站首页>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 6174
As difference occurs, see example for output format.Note that each number is output as 4
digits.
Input Sample 1:
6767
Output Sample 1:
7766 - 6677 = 10899810 - 0189 = 96219621 - 1269 = 83528532 - 2358 = 6174
Input Sample 2:
2222
Output 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 <
边栏推荐
猜你喜欢
百行代码发射红心,程序员何愁命不中女朋友!
Assembly language (8) x86 inline assembly
Concurrent CAS
Why is pnpm hitting npm and yarn dimensionality reduction?
sql server中 两表查询 平均数 分组
苹果官网商店新上架Mophie系列Powerstation Pro、GaN充电头等产品
SQL语句查询字段内重复内容,并按重复次数加序号
MySQL内部函数介绍
MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
CCVR基于分类器校准缓解异构联邦学习
随机推荐
Why is pnpm hitting npm and yarn dimensionality reduction?
thinkPHP5 实现点击量(数据自增/自减)
树状数组模版+例题
mySQL数据库初始化失败,有谁可以指导一下吗
512-color chromatogram
MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
16种香饭做法全攻略
selectPage 动态改变参数方法
Weekly Report 2022-8-4
Creo 9.0 基准特征:基准点
ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...
按钮上显示值的轮流切换
Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
The difference between beautiful MM and ordinary MM
只有一台交换机,如何实现主从自动切换之nqa
Why do I recommend using smart async?
线程之Happens-before规则
动态内存开辟(C语言)