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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- js 循环list
$.ajax({ type : "POST", data:{ createStartTime:'', createEndTime:'' }, url : "<%=r ...
- python笔记-文件读写
文件操作过程一般为:打开.读写.关闭: 打开:open()或file() 读写:read().write(): 关闭:close(): 1.打开:open()或file() file_handler= ...
- JUnit单元测试代码
package com.storage.test; import org.junit.Before; import org.junit.Test; import org.springframework ...
- [CSS] Frequently used method or solutions for issues
Stick button in right side in html Solution: //In the html <div class="float__button" & ...
- ubuntu14安装mantis实践(包含LAMP/PHP)
安装LAMP 参考 https://www.linuxidc.com/Linux/2016-12/138757.htm sudo add-apt-repository ppa:ondrej/apach ...
- 15.IEnumerable和IEnumerator
先说IEnumerable,我们每天用的foreach你真的懂它吗? 阅读目录 自己实现迭代器 yield的使用 怎样高性能的随机取IEnumerable中的值 我们先思考几个问题: 为什么在fore ...
- random函数的使用
random作为随机函数用处很多,在Python里面也经常使用,特别是处理一些随机事件的时候,特别好用! 废话不多说,直接看下面的例子: random.randomrandom.random() ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:建立一个EF数据模型
英文渣水平,大伙凑合着看吧…… 这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第一篇: ...
- 记录一则xtts测试遇到的诡异现象
背景:在一次xtts的测试中遇到因源库数据文件名称包含特殊字符导致表空间全量备份缺失文件,之所以说是诡异现象,是因为xtts的全备日志不报任何错误,在恢复阶段才发现缺少文件,这个缺陷比较隐晦,尤其在迁 ...
- Gitlab安装以及汉化
Gitlab安装以及汉化 系统环境: CentOS 7.5 IP:192.168.1.2 关闭selinux.firewalld gitlab-ce-10.8.4 rpm包:下载地址 一.下载并安装g ...