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. 九 fork/join CompletableFuture

    1: Fork/join fork/join:  fork是分叉的意思, join是合并的意思. Fork/Join框架:是JAVA7提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务 ...

  2. DevExpress 显示进度条

    1.使用了DevExpress的WaitDialogForm WaitDialogForm waitDialogForm = null; new Thread((ThreadStart)delegat ...

  3. fabric差异化部署mysql和lnmp

    1.代码如下: vim lnmp.py ------------------------------------------> #!/usr/bin/env python from fabric ...

  4. C#笔记(二)

    转换操作符:操作符重载,可自定义实现从一种类型到另一种类型的显示或者隐式转换 : true/false也可进行操作符重载: LINQ中大部分查询运算符都有一个非常重要的特性:延迟执行.这意味着,他们不 ...

  5. 《精通Spring4.X企业应用开发实战》读后感第七章(AOP概念)

  6. Git merge一个branch到另一个branch

    在项目开发过程中,需要merge一个branch (branch名 taskBranch) 到另一个名为develop 的branch 方法: 先保证当前停留在develop的branch上 然后执行 ...

  7. 4.Windows应急响应:勒索病毒

    0x00 前言 勒索病毒,是一种新型电脑病毒,主要以邮件.程序木马.网页挂马的形式进行传播.该病毒性质恶劣. 危害极大,一旦感染将给用户带来无法估量的损失.这种病毒利用各种加密算法对文件进行加密,被感 ...

  8. hdu1062

    #include<stdio.h> #include<string.h> int main() { int i,n,len,j,k,t; char s1]; scanf(&qu ...

  9. Java8 使用 stream().map()提取List对象的某一列值及排重

    List对象类(StudentInfo) public class StudentInfo implements Comparable<StudentInfo> { //名称 privat ...

  10. 在 windows 下搭建 IDEA + Spark 连接 Hive 的环境

    为了开发测试方便,想直接在 IDEA 里运行 Spark 程序,可以连接 Hive,需不是打好包后,放到集群上去运行.主要配置工作如下: 1. 把集群环境中的 hive-core.xml, hdfs- ...