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. JAVA 1.5 并发之 Executor框架 (内容为转载)

    本文内容转自 http://www.iteye.com/topic/366591 Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Exec ...

  2. 字符编码ASCII、Unicode、GB

    计算机的存储都是二进制的,那么我们平时看到的各种字符都需要通过按照一定的格式转换成为二进制才能在被计算机识别与处理.这个过程便成为编码.常见的编码方式有ASCII.Unicode.GB2312等. 1 ...

  3. .net关于应用程序缓存的一些疑惑

    疑惑:获取缓存后强制转换为实体对象传递给前台,如果前台对这个实体对象中属性更改的话缓存中的数据也随之改变,为啥??? 首先是创建缓存的方法: /// <summary> /// 创建缓存项 ...

  4. appium+python+jenkins+selenium grid+unnittest+生成报告打造UI自动化回归、监控体系

    先放一下截图,展示一下平台做成的样子,以及实现后的结果,后面贴上自动化用例执行的过程中,帮我们发现的线上问题 关于appium自动化环境的安装,网上有很多教程,我就不重复赘述,后面陆续写出设计思想,贴 ...

  5. idea中,使用Gradle创建的项目,如何变为web项目

    当idea开发项目时,使用gradle构建项目,包引用完后,发现idea并没有正确识别项目为web项目. 主要有两点表现: 1. src/main/resources的resources目录没有或有但 ...

  6. 关于奇异值分解(SVD)的理解

    奇异值分解实际上是将一个矩阵,分解成为两个不同维度(行数和列数)上的正交向量集之间的映射变换,奇异值则是变换时的缩放! 例如上面的矩阵M就是一个5维映射到4维的变换矩阵,而SVD分解得到的奇异值和奇异 ...

  7. netty对http协议解析原理解析(转载)

    本文主要介绍netty对http协议解析原理,着重讲解keep-alive,gzip,truncked等机制,详细描述了netty如何实现对http解析的高性能. 1 http协议 1.1 描述 标示 ...

  8. linux下编译Boost库

    下载源码 boost_1_66_0.tar.gz 生成编译工具 # tar axf boost_1_66_0.tar.gz # cd boost_1_66_0 # yum install gcc gc ...

  9. Beta冲刺测试

    1.项目概述 1.项目名称 微信四则运算小程序 2.项目简介 基于微信小程序,为用户提供一个答题的平台 3.项目预期达到目标 用户通过微信小程序可以在里边答题,模式或者题量的选择为用户匹配到适合他们的 ...

  10. 51nod 1154【DP】

    区间DP大暴力吧?GG. dp[ i ] 为字符至 i 的最少数量. 如果[Left , Right]是回文串, dp[Right] = min(dp[ Right ] , dp[Left-1] + ...