当前位置:网站首页>PAT乙级-B1019 数字黑洞(20)
PAT乙级-B1019 数字黑洞(20)
2022-08-05 09:05:00 【nekoha_dexter】
给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。
例如,我们从6767开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。
输入格式:
输入给出一个 (0,104) 区间内的正整数 N。
输出格式:
如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。
输入样例 1:
6767
输出样例 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
输入样例 2:
2222
输出样例 2:
2222 - 2222 = 0000#include<iostream>
#include<algorithm>
using namespace std;
string s1,s2;
int main(){
cin >> s1;
int ans;
//s1可能不满4位,应当插入0
s1.insert(0,4-s1.size(),'0');
do{
sort(s1.begin(), s1.end());
s2 = s1;
//s1此时从大到小
reverse(s1.begin(), s1.end());
cout <<s1 << " - " << s2 <<" = ";
ans = stoi(s1) - stoi(s2);
s1 = to_string(ans);
//补足4位
s1.insert(0,4-s1.size(),'0');
cout << s1 << endl;
}while(ans != 0 && ans != 6174);
return 0;
}
边栏推荐
猜你喜欢

百行代码发射红心,程序员何愁命不中女朋友!

Example of Noise Calculation for Amplifier OPA855

放大器OPA855的噪声计算实例

ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...

ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色

工程制图知识点

Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上

Creo 9.0 基准特征:基准点

Code Audit - PHP

【LeetCode】623. Add a row to the binary tree
随机推荐
pnpm 是凭什么对 npm 和 yarn 降维打击的
这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
工程制图直线投影练习
只有一台交换机,如何实现主从自动切换之nqa
(转)[Json]net.sf.json 和org.json 的差别及用法
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
微信小程序请求封装
Excuse me, guys, is it impossible to synchronize two databases in real time using Flink SQL CDC?
HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
Luogu P3368: 【模板】树状数组 2
Example of Noise Calculation for Amplifier OPA855
tensorflow.keras cannot introduce layers
There is only one switch, how to realize the nqa of master-slave automatic switching
我的杂记链接
Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood
【ASM】字节码操作 方法的初始化 Frame
seata源码解析:事务状态及全局锁的存储
路由----router