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:

  1. 6767

Sample Output 1:

  1. 7766 - 6677 = 1089
  2. 9810 - 0189 = 9621
  3. 9621 - 1269 = 8352
  4. 8532 - 2358 = 6174

Sample Input 2:

  1. 2222

Sample Output 2:

  1. 2222 - 2222 = 0000

提交代码

  1. #include<cstdio>
  2. #include<stack>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<stack>
  6. #include<set>
  7. #include<map>
  8. using namespace std;
  9. int dight[];
  10. int main()
  11. {
  12. //freopen("D:\\INPUT.txt","r",stdin);
  13. int n,maxnum,minnum;
  14. scanf("%d",&n);
  15. int i,j;
  16. do{//有可能n一开始就是6174
  17. for(i=; i>=; i--)
  18. {
  19. dight[i]=n%;
  20. n/=;
  21. }
  22. for(i=; i<; i++)//由大到小
  23. {
  24. maxnum=i;
  25. for(j=i+; j<; j++)
  26. {
  27. if(dight[j]>dight[maxnum])
  28. {
  29. maxnum=j;
  30. }
  31. }
  32. int t=dight[maxnum];
  33. dight[maxnum]=dight[i];
  34. dight[i]=t;
  35. }
  36. minnum=;
  37. for(i=; i>=; i--)
  38. {
  39. minnum*=;
  40. minnum+=dight[i];
  41. }
  42. maxnum=;
  43. for(i=; i<; i++)
  44. {
  45. maxnum*=;
  46. maxnum+=dight[i];
  47. }
  48. n=maxnum-minnum;
  49. printf("%04d - %04d = %04d\n",maxnum,minnum,n);
  50. if(!n)
  51. break;
  52. }while(n!=);
  53. return ;
  54. }

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. mina中的发送延时

    由于项目需要,用到了 mina 框架进行 tcp 通讯.我是初次接触 mina,于是从 Hello world 开始学习了 mina .期间遇到了一个奇怪的发送数据的延迟问题,解决的过程是曲折的,但找 ...

  2. 安装mariadb并修改配置文件

    实验环境:CentOS7 #安装mariadb-server包#修改mariadb配置文件/etc/my.cnf.d/server.cnf#添加 skip_name_resolve=ON #不执行将I ...

  3. 4.JasperReports学习笔记4-查询数据库生成动态的报表(WEB)

    转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html 第一种方式: sql语句中定义查询条件,报表中定义接收参数 第二种方式: ...

  4. 调试opencv调用摄像头程序时碰到的问题

    昨天晚上想把opencv学习笔记整理一下,当跑opencv调用摄像头的程序的时候老是出现Assertion failed (size.width>0 && size.height ...

  5. 差一点搞混了Transactional注解

    今天给我的Srping业务层加如下Service和Transactional注解: @Service @Scope(BeanDefinition.SCOPE_SINGLETON) @Transacti ...

  6. Java探索之旅(1)——概述与控制台输入

    使用的课本: Java语言程序设计(基础篇)----西电 李娜(译) 原著: Introduction to Java Progrmming(Eighth Edition) -----Y.Daniel ...

  7. shell批量创建文件及改名

    批量创建文件及改名企业面试题2:使用for循环在/usr/sunzy目录下通过随机小写10个字母,批量创建10个html文件. #!/bin/bash Path=/usr/sunzy [ -d $Pa ...

  8. Java之反射(部分文档摘过来方便以后查看)

    第1章 类加载器 1.1 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. l 加载 就是指将class文件读入内存,并为之创建 ...

  9. 6.JBoss5.x6.x 反序列化漏洞(CVE-2017-12149)复现

    2017 年 9 月 14 日,国家信息安全漏洞共享平台( CNVD )收录了 JBOSS Application Server 反序列化命令执行漏洞( CNVD-2017-33724,对应 CVE- ...

  10. hdu1069

    #include <iostream> #include <algorithm> #include <cstring> using namespace std; c ...