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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- java并发包消息队列(也即阻塞队列BlockingQueue)
下面是典型的消息队列的生产者与消费者模式的例子
- GreenDao开源ORM框架浅析
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011133213/article/details/37738943 Android程序开发中,避免 ...
- C#基础加强(4)之秒懂IL、CTS、CLS和CLR
IL(Intermediate Language) 中间语言..Net 平台下不只有 C# 语言,还有 VB.Net.F# 等语言.IL 是程序最终编译的可执行二进制代码(托管代码),类似于 Java ...
- java框架之SpringBoot(10)-启动流程及自定义starter
启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...
- mysql 日期相关 CURRENT_TIMESTAMP, CURRENT_DATE, CURRENT_TIME
MySQL 时间函数 SELECT CURRENT_TIMESTAMP, CURRENT_DATE, CURRENT_TIME, CURRENT_TIME(), NOW(); https://dev. ...
- python安装包API文档
在python开发过程中,经常会使用第三方包,或者内置的包. 那么这些包,具体有哪些选项,有哪些方法,你知道吗?下面介绍一种万能方法. 使用命令:<注意,命令里python显示的API版本是根据 ...
- Solr数据迁移
单机Solr部署在linux /opt目录下,运行一段时间后发现该目录分配的空间不足,而Solr的索引数据量较大,必须更改相关core下面的data目录,以改变索引存放的目录. 找到相应的solrco ...
- js对象属性名驼峰式转下划线
一.题目示例: 思路: 1.匹配属性名字符串中的大写字母和数字 2.通过匹配后的lastIndex属性获取匹配到的大写字母和数字的位置 3.判断大写字母的位置是否为首位置以及lastIndex是否为0 ...
- 第八篇——Struts2的处理结果类型
Struts2处理结果类型 1.SUCCESS:表示Action正确的执行完成,返回相应的视图,success是name属性的默认值: 2.ERROR:表示Action执行失败,返回到错误处理视图: ...
- shell printf命令:怎样格式化输出语句
printf 命令用于格式化输出, 是echo命令的增强版.它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同. 注意:printf 由 POSIX 标准所定义,移植性要比 ech ...