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. VBS调用并监控记事本进程

    Dim flag flag=true Set WshShell = CreateObject("WScript.Shell") '创建WScript.Shell对象     Set ...

  2. 使用pygame制作一个简单的游戏

    翻译自Will McGugan的<Beginning Game Development with Python and Pygame –From Novice to Professional&g ...

  3. PCLVisualizer可视化类(2)

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=163 可视化点云颜色特征 所示,点赋予不同的颜色表征其对应的z轴值不同.PC ...

  4. macos下清除dnscache

    sudo killall -HUP mDNSResponder 参见链接

  5. Entity Framework Code-First(9):DataAnnotations

    DataAnnotations in Code-First: EF Code-First provides a set of DataAnnotation attributes, which you ...

  6. 高性能服务器设计(Jeff Darcy's notes on high-performance server design

    高性能服务器设计(Jeff Darcy's notes on high-performance server design 我想通过这篇文章跟大家共享一下我多年来怎样开发“服务器”这类应用的一些想法和 ...

  7. 多值cookie

  8. HTML5学习笔记(七)WebSocket

    WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议.在WebSocket API中,浏览器和服务器只需要做一个握手的动作 浏览器通过 JavaScript 向服务器 ...

  9. C++内存管理之unique_ptr

    一个unique_ptr"拥有“他所指向的对象.与shared_ptr不同,某个时刻只能有一个unique_ptr指向一个给定的对象.当unique_ptr被销毁时,它所指向的对象也被销毁. ...

  10. Node.js 内置模块crypto使用事件方法(onreadable)加密的一些问题

    javaScript代码如下: 'use strict'; const crypto = require('crypto'); //实例化一个AES加密对象 const aesEncrept = cr ...