当前位置:网站首页>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;
}
边栏推荐
- Assembly language (8) x86 inline assembly
- DTcloud 装饰器
- Excuse me if you want to write data in mysql, with flink - connector - JDBC directly is ok, but I'm in the f
- Moonbeam团队发布针对整数截断漏洞的紧急安全修复
- 吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
- How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
- 最 Cool 的 Kubernetes 网络方案 Cilium 入门教程
- 汇编语言(8)x86内联汇编
- 并发之CAS
- 明天去订票,准备回家咯~~
猜你喜欢

【Excel实战】--图表联动demo_001

Redis cache and existing problems--cache penetration, cache avalanche, cache breakdown and solutions

Detailed explanation of DNS query principle

sql server中 两表查询 平均数 分组

基于 Kubernetes 的微服务项目整体设计与实现

让程序员崩溃的N个瞬间(非程序员误入)

全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了

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

链表中的数字相加----链表专题

php fails to write data to mysql
随机推荐
sql server中 两表查询 平均数 分组
love is a sad song
leetcode 剑指 Offer 10- I. 斐波那契数列
Detailed explanation of DNS query principle
egg framework
周报2022-8-4
汇编语言(8)x86内联汇编
sphinx matches the specified field
How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
flink cdc支持从oracle dg库同步吗
ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
SQL语句查询字段内重复内容,并按重复次数加序号
宝塔实测-搭建中小型民宿酒店管理源码
干货!生成模型的评价与诊断
Rotation of the displayed value on the button
Overall design and implementation of Kubernetes-based microservice project
ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)
树状数组模版+例题