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 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 (0, 10000).
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 <iostream>
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"algorithm"
using namespace std;
bool compareChar(char c1, char c2){
if(c1<c2)
return false;
return true;
}
char * stringMinus(char *s1, char *s2){
for(int i=3;i>=0;i--){
if(s1[i]>=s2[i]){
s1[i] = '0'+(s1[i]-s2[i]);
}
else{
s1[i] = '0'+(s1[i]-s2[i]+10);
if(i>0)
s1[i-1] -= 1;
}
}
return s1;
} int main()
{
int n;
char s[5]="0000";
char incr[5];
scanf("%d",&n);
int i=0;
while(n){
int x = n%10;
s[3-i] = '0'+x;
n/=10;
i++;
}
while(strcmp(s,"0000")!=0){
sort(s,s+4);
strcpy(incr,s); sort(s,s+4,compareChar); printf("%s - %s = ",s,incr);
printf("%s\n",stringMinus(s,incr));
if(strcmp(s,"6174")==0){
break;
}
}
return 0;
}
1069. The Black Hole of Numbers (20)的更多相关文章
- 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 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 ...
- 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特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【PAT甲级】1069 The Black Hole of Numbers (20 分)
题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...
- 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 ...
随机推荐
- Java Server returned HTTP response code: 401
今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的.写好之后请求,返回异常Java Server returned HTTP response code: ...
- 出来ios顶部导航掉下来问题
<script type="text/javascript"> setposition(); function setposition(){ var ua = navi ...
- 【项目经验】--EasyUI DataGrid之右键菜单
前两天验收项目,老总提了一个不是需求的需求,为什么这么说呢?因为我们的管理不到位!话说当天,我们UI系统下发了一个总文件,上面写着"各个系统找一个没有添加UI的模块去添加最新版本UI进行测试 ...
- kafka producer源码
producer接口: /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor l ...
- Android:res之shape制作圆角、虚线、渐变
xml控件配置属性 android:background="@drawable/shape" 标签 corners ----------圆角gradient ----------渐 ...
- 解读Web Page Diagnostics网页细分图
解读Web Page Diagnostics网页细分图 http://blog.sina.com.cn/s/blog_62b8fc330100red5.html Web Page Diagnostic ...
- Mongdb使用客户端
安装Robomongo图形化管理工具 Robomongo是一个基于 Shell 的跨平台开源 MongoDB 管理工具.嵌入了 JavaScript 引擎和 MongoDB mogo . 只要你会使用 ...
- PHP入门 - - 07-->HTML的表单
一.<form>标签及其属性 <from></form>标签对用来创建一个表单,即定义表单的开始和结束位置,<form>标签具有下面等属性. ...
- vim使用02
编辑 剪切光标所在的字符: <x>; 剪切并插入: <s> 撤销操作:撤销至上一个命令之间的修改: <u> 恢复上一次撤销操作: <C r> 剪切光标所 ...
- BZOJ4360 : achievement
对于$mode=0$的情况: 假设已经知道了最终要做哪些成就,那么这些成就一定是按$b$递减做的. 将成就按$b$从大到小排序,考虑往已选集合里新加一个成就. 假设该成就前面有$t$个已选成就,后面成 ...