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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 1、js比较日期的大小
① html <div class="ptb10"><span>共享开始时间:</span><input type="text& ...
- Selenium 2(Webdriver)
如何用webdriver打开一个浏览器,我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器,很多新的特性都会在firefox中体现.但是做页面的测试,启 ...
- SpringBoot-将servlet容器变成undertow测试tomcat吞吐量
将Servlet容器变成Undertow 默认情况下,Spring Boot 使用 Tomcat 来作为内嵌的 Servlet 容器 可以将 Web 服务器切换到 Undertow 来提高应用性能.U ...
- python练习题-day11
1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), 要求:登录成功一次,后续的函数都无需再输入用户名和密码 flag=False def wrapper(fun): def inn ...
- linux 通过pid 寻找程序路径的最简单命令(pwdx)
在linux实际操作命令中,查看pid的方式有很多种,通过pid找程序路径的方式也有好几个,但是可能大家都忽略的一个很简单也是很实用的命令:pwdx. 比如要查找某个java编写的程序运行情况可通过j ...
- GlusterFS配置及使用
一.GlusterFS 配置及使用 GlusterFS配置及使用:https://www.cnblogs.com/sxchengchen/p/7805667.html 二.CentOS 7 安装部署 ...
- linux环境如何配置repo
(1)下载repo mkdir ~/bin curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo ...
- 问题: 揭秘Angualr2 书上问卷调查
npm install 初夏下面问题: 0 info it worked if it ends with ok1 verbose cli [ '/home/linux_ubuntu164/tools/ ...
- CSS组合选择符
组合选择符说明了两个选择器直接的关系. 目录: 后代选取器(以空格分隔) 子元素选择器(以大于号分隔) 相邻兄弟选择器(以加号分隔) 普通兄弟选择器(以破折号分隔) 后代选取器 后代选取器匹配所有值得 ...
- Vue系列之 => ref获取DOM元素和组件
可以获取DOM元素,和组件中的数据,方法 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...