当前位置:网站首页>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;
}
边栏推荐
- Luogu: P2574 XOR的艺术 [线段树]
- Going to book tickets tomorrow, ready to go home~~
- How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
- Creo 9.0 基准特征:基准坐标系
- ts/js 函数传参带函数写法
- Overall design and implementation of Kubernetes-based microservice project
- sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
- The toss of MM before going to the street (interesting)
- ts/js function pass parameter with function writing
- express hot-reload
猜你喜欢
使用 External Secrets Operator 安全管理 Kubernetes Secrets
SQL语句查询字段内重复内容,并按重复次数加序号
Creo 9.0 基准特征:基准点
The Coolest Kubernetes Network Solution Cilium Getting Started Tutorial
CCVR基于分类器校准缓解异构联邦学习
sql server中 两表查询 平均数 分组
express hot-reload
全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
最 Cool 的 Kubernetes 网络方案 Cilium 入门教程
随机推荐
Code Audit - PHP
tensorflow.keras cannot introduce layers
8.4 Summary of the mock competition
程序员的七种武器
Linux导出数据库数据到硬盘
Creo 9.0 基准特征:基准点
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
How Entrepreneurs Attract Venture Capitalists
Rotation of the displayed value on the button
这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
mySQL数据库初始化失败,有谁可以指导一下吗
Chapter 12 Bayesian Networks
Why is pnpm hitting npm and yarn dimensionality reduction?
XSS靶机通关以及XSS介绍
十一道家常小菜详细攻略[图文并茂]
How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
让硬盘更快,让系统更稳定
交换机端口的三种类型详解与hybrid端口实验
Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood