1069. The Black Hole of Numbers (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<cstdio>
#include<stack>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
int dight[];
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int n,maxnum,minnum;
scanf("%d",&n);
int i,j;
do{//有可能n一开始就是6174
for(i=; i>=; i--)
{
dight[i]=n%;
n/=;
}
for(i=; i<; i++)//由大到小
{
maxnum=i;
for(j=i+; j<; j++)
{
if(dight[j]>dight[maxnum])
{
maxnum=j;
}
}
int t=dight[maxnum];
dight[maxnum]=dight[i];
dight[i]=t;
}
minnum=;
for(i=; i>=; i--)
{
minnum*=;
minnum+=dight[i];
}
maxnum=;
for(i=; i<; i++)
{
maxnum*=;
maxnum+=dight[i];
}
n=maxnum-minnum;
printf("%04d - %04d = %04d\n",maxnum,minnum,n);
if(!n)
break;
}while(n!=);
return ;
}

pat1069. The Black Hole of Numbers (20)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 1069 The Black Hole of Numbers (20分)

    1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. PAT1069. The Black Hole of Numbers

    //这是到水题,之前因为四位数的原因一直不能A,看了别人的程序,才明白,不够四位的时候没考虑到,坑啊.....脸打肿 #include<cstdio>#include<algorit ...

  8. PAT (Advanced Level) 1069. The Black Hole of Numbers (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  9. PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. netty中的ByteBuf

    网络数据的基本单位总是字节.Java NIO 提供了 ByteBuffer 作为它 的字节容器,但是这个类使用起来过于复杂,而且也有些繁琐. Netty 的 ByteBuffer 替代品是 ByteB ...

  2. DTP模型之一:(XA协议之一)XA协议、二阶段2PC、三阶段3PC提交

    XA协议 XA是一个分布式事务协议,由Tuxedo提出.XA中大致分为两部分:事务管理器和本地资源管理器.其中本地资源管理器往往由数据库实现,比如Oracle.DB2这些商业数据库都实现了XA接口,而 ...

  3. lyui 列表 上传

    1.js layui.use(['table', 'element', 'laydate', 'layer','upload'], function () { var table = layui.ta ...

  4. 树莓派 Learning 002 装机后的必要操作 --- 02 解决中文问题

    树莓派 装机后的必要操作 - 解决中文问题 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 每一块树莓派,装机后都应该执行的步骤 刚装机后, ...

  5. p2320&bzoj1192 鬼谷子的钱袋

    传送门(洛谷) 传送门(bzoj) 题目 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一 ...

  6. POJ 2398 Toy Storage (叉积判断点和线段的关系)

    题目链接 Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4104   Accepted: 2433 ...

  7. HDU 3400 Line belt (三分嵌套)

    题目链接 Line belt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. SNAT端口转发配置

    需求说明 在只有外网地址的机器上也能正常访问内网地址 配置过程 环境网络信息 网络名称 网络地址 外网 192.168.200.0/24 (网关:192.168.200.251) 内网 92.0.0. ...

  9. 【WIP】客户端JavaScript 事件处理

    创建: 2017/10/15 完成: 2017/10/15   更新: 2017/11/04 加粗事件的参数 更新: 2017/12/12 增加事件处理时获取事件对象的方法 更新: 2019/05/2 ...

  10. C#报算术运算导致溢出的错误

    如果是在fill datatable时报这个错,是因为所查询的数据中有在数据库中为NUMBER类型的字段,这种类型C#中不能识别,需TO_CHAR转换成字符型,就不会再报这个错了. e.g. SELE ...