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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- PHP调用接口用post方法传送json数据
1.核心代码: <?php require("helper.php"); header('content-type:text/html;charset=utf-8'); $k ...
- 深入浅出--UNIX多进程编程之fork()函数
0前言 上周都在看都在学习unix环境高级编程的第八章--进程控制.也就是这一章中.让我理解了unix中一些进程的原理.以下我就主要依照进程中最重要的三个函数来进行解说.让大家通过阅读这一篇文章彻底明 ...
- git常用命令--tag
/// 查看标签 // 打印所有标签 git tag // 打印符合检索条件的标签 git tag -l 1.*.* // 查看对应标签状态 git checkout 1.0.0 /// 创建标签(本 ...
- [py]GTM和UTC及python的时间戳
时间戳是一串字符串 time.time() 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数.通俗的讲, 时间戳是一 ...
- Hadoop生态集群MapReduce详解
一.概述 MapReduce是一种编程模型,这点很重要,仅仅是一种编程的模型,而不是具体的软件.在hadoop中,HDFS是分布式的文件存储系统,而MapReduce是一个分布式的计算框架.用于大规模 ...
- phpstudy----------如何将phpstudy里面的mysql升级到指定版本,如何升级指定PHP版本
1.下载指定版本:从官网上下载高版本的 MySQL :https://dev.mysql.com/downloads/file/?id=467269,选的版本是 5.7.17 2.请注意第四部以前是可 ...
- SV coverage
covergroup是对coverage model的一种包装,每个covergroup可以包含: 1) sync event来触发采样, 2) 很多coverpoint, 3) cross cove ...
- 玩转spring boot——war部署
前言 之前部署spring boot应用是通过直接输入命令“java -jar”来实现的.而有些情况,由于部署环境的制约,只能把项目从jar转换成war才能部署,如新浪云sae的java环境容器.那怎 ...
- laravel容器container 阅读记录
今天抽时间又仔细看了一下laravel的container,记录一下. 所谓容器,听名字就知道,是一个仓库,装东西用的,所以,container所有的功能,都围绕一个主题:管理装. 类名称:Illum ...
- JavaScript 神奇的参数
JS函数的参数,和其他语言区别非常大.它不在乎你传过来多少个参数,也不在乎传过来的参数是什么类型.即使你定义的函数只接受两个参数,你调用这个函数的时候可以传递一个.三个甚至不传参数.这是因为JavaS ...