PAT 1069 The Black Hole of Numbers
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174
-- the black hole of 4-digit numbers. This number is named Kaprekar Constant.
For example, start from 6767
, we'll get:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range (.
Output Specification:
If all the 4 digits of N are the same, print in one line the equation N - N = 0000
. Else print each step of calculation in a line until 6174
comes out as the difference. All the numbers must be printed as 4-digit numbers.
Sample Input 1:
6767
Sample Output 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
Sample Input 2:
2222
Sample Output 2:
2222 - 2222 = 0000
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; bool comp(char a,char b){
return a>b;
} int to_int(string s){
int sum = ;
for(int i=;i < s.size();i++){
int num = s[i] - '';
sum = sum* + num;
}
return sum;
} string minuss(string s1,string s2){
string res;
int a = to_int(s1);
int b = to_int(s2);
int c = a - b;
res = to_string(c);
int len = res.size();
for(int i=;i < (-len);i++){ //加前导0;
res = ""+res;
}
return res;
} int main(){ string s;cin >> s;
int len = s.size();
for(int i=;i < (-len);i++){ //加前导0;
s = ""+s;
}
string s1 = s,s2 = s;
sort(s1.begin(),s1.end(),comp);
sort(s2.begin(),s2.end());
string ans = minuss(s1,s2);
// cout << ans;
if(ans == ""){
printf("%s - %s = %s\n",s1.c_str(),s2.c_str(),ans.c_str());
return ;
}
else{
printf("%s - %s = %s\n",s1.c_str(),s2.c_str(),ans.c_str());
} while(ans!=""){
s1 = ans; s2 = ans;
sort(s1.begin(),s1.end(),comp);
sort(s2.begin(),s2.end());
ans = minuss(s1,s2);
printf("%s - %s = %s\n",s1.c_str(),s2.c_str(),ans.c_str());
} return ;
}
贼弱智,说是四位数,你给个0~10000范围,补前导0还算数字,服了
PAT 1069 The Black Hole of Numbers的更多相关文章
- PAT 1069 The Black Hole of Numbers[简单]
1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...
- pat 1069 The Black Hole of Numbers(20 分)
1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...
- PAT 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- 1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise
题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit inte ...
- PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)
1069 The Black Hole of Numbers (20 分) For any 4-digit integer except the ones with all the digits ...
- 1069 The Black Hole of Numbers (20分)
1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...
- PAT Advanced 1069 The Black Hole of Numbers (20) [数学问题-简单数学]
题目 For any 4-digit integer except the ones with all the digits being the same, if we sort the digits ...
- PAT (Advanced Level) 1069. The Black Hole of Numbers (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 学习笔记:Makefile的ifeq逻辑或,逻辑与的变通实现
(1)ifeq的用法 ifeq ($(变量名), 变量值 ) ........ else ifeq ($(..), ..) ......... else ......... endif (2) ...
- Fiddler修改图片显示
培训课讲修改请求值,记录一下操作步骤: 步骤如下: 1. 点击人人网图片另存为到桌面 2. 打开fiddler,找到图片发送的请求(单击图片的链接,点击右边面板的Inspectors.查看ImageV ...
- webstorm 配置 开发微信小程序
默认情况下,webstorm是不支持wxml和wxss的文件类型,不会有语法高亮 设置高亮 除了高亮,还需要代码提示, 所幸已经有前辈整理了小程序的代码片段,只需要导入其安装包即可使用,包文件路径如下 ...
- FastDFS的单点部署
1 安装libfastcommon 注意:在Centos7下和在Ubuntu下安装FastDFS是不同的,在Ubuntu上安装FastDFS需要安装libevent,而外Centos上安装FastD ...
- 【UML】NO.54.EBook.6.UML.2.002-【Thinking In UML 大象 第二版】- UML 核心元素
1.0.0 Summary Tittle:[UML]NO.54.EBook.6.UML.2.002-[Thinking In UML 大象 第二版]- UML 核心元素 Style:DesignPat ...
- select2 api参数中文文档
select2 api参数的文档 具体参数可以参考一下: 参数 类型 描述 Width 字符串 控制 宽度 样式属性的Select2容器div minimumInputLength int 最小数 ...
- [iOS] 测试设备解决自签名证书问题
不多说,解决过程都是泪. 用了最简单粗暴的方式. 1. 将你的自签名证书,放到测试设备可以访问的站点上 2. 用safari访问上面的地址,直接将证书安装到本设备上 搞掂! Have fun with ...
- [openjudge-搜索]湖的深度
题目描述 描述 一个湖用 R x C (1 ≤ R ≤ 50; 1 ≤ C ≤ 50) 的网格表示.格点上的非负整数 D_rc (0 ≤ D_rc ≤ 1,000,000)表示该位置的深度.整数0表示 ...
- Redis初探-Redis安装
官网地址:https://redis.io/download 最新版本是4.0,在这里本人下的是3.2 使用rz命令可以将Redis上传到Linux系统 首先要确定Linux上是否安装了gcc,没有则 ...
- windbg无故不显示command窗口
原文最早发表于百度空间2010-02-05 有的dump可以显示,有的不行……上网找了一通没有收获,自己搞了一下,终于在点击“window”——“cascade floating windows”后出 ...