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


提交代码

 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[4];
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=3; i>=0; i--)
18         {
19             dight[i]=n%10;
20             n/=10;
21         }
22         for(i=0; i<4; i++)//由大到小
23         {
24             maxnum=i;
25             for(j=i+1; j<4; 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=0;
37         for(i=3; i>=0; i--)
38         {
39             minnum*=10;
40             minnum+=dight[i];
41         }
42         maxnum=0;
43         for(i=0; i<4; i++)
44         {
45             maxnum*=10;
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!=6174);
53     return 0;
54 }

转载于:https://www.cnblogs.com/Deribs4/p/4780414.html

pat1069. The Black Hole of Numbers (20)相关推荐

  1. pat-1069 The Black Hole of Numbers (20分)

    感觉我的解答比很多题解都好,嘻嘻就是模板的使用 但是需要去做注意一个测试点,如果输出的数字是一个小于1000的数,就是数位小于4,那么就需要自己去使用string的匿名函数去补上前导0 代码如下: # ...

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

  3. 【PAT甲级 前导0,排序】1069 The Black Hole of Numbers (20 分) C++ 全部AC

    题目 一直循环相减即可,直到结果为6174或者四个数字相同就结束循环 题解 C++ #include<iostream> #include<stdio.h> #include& ...

  4. 15行代码AC——1019 数字黑洞 (20分) 甲级1069. The Black Hole of Numbers (20)(解题报告)

    立志用更少的代码做更高效的表达 PAT甲级最优题解-->传送门 Pat乙级最优化代码+题解+分析汇总-->传送门 给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非 ...

  5. PAT1069. The Black Hole of Numbers

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

  6. 【PAT (Advanced Level) Practice】1120 Friend Numbers (20 分)

    1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...

  7. How to Pronounce Numbers 20 – 1 Billion

    How to Pronounce Numbers 20 – 1 Billion Share Tweet Share Tagged With: Numbers Numbers are something ...

  8. PAT甲级1069 The Black Hole of Numbers:[C++题解]模拟、6174

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 注意特判 6174这个数:if(n == 6174) printf("7641 - 1467 = 6174\n" ...

  9. pat1100. Mars Numbers (20)

    1100. Mars Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue People o ...

最新文章

  1. Apache TinkerPop毕业成为顶级项目
  2. 腾讯产品经理的一天是啥样的?
  3. sql嵌套查询返回多个字段_list4 SQL复杂查询
  4. Youtube Links
  5. MATLAB bwlabel
  6. OPIE(火狐扩展)-导入导出firefox扩展的所有设置
  7. VS2015安装包损坏或丢失的方法_2022年7月
  8. 苹果安装app的另一种方式(通过itms-services协议,不通过AppStore,直接安装IOS应用程序)
  9. word中html在哪,Win10 word模板路径在哪?Win10如何修改word模板路径?
  10. Qemu core 调试Cannot access memory at address 0x7fbc6c792858
  11. OpenLayers添加地图标记
  12. 最近在读的一些文章-2019.04
  13. J-link OB 读取不了的问题
  14. 区块链,屌丝逆袭的最后机会
  15. linux查看tomcat在,在陌生Linux环境查看Tomcat服务的方法
  16. 利用Java语言编写一个猜数字游戏(有次数限制)
  17. 计算机辅助项目管理实验论文,计算机辅助项目管理B卷
  18. navicat与mysql
  19. C++找文件夹中,最长的名字有多长
  20. 关于Manifest

热门文章

  1. 螺旋千斤顶设计大作业_如何正确使用汽车千斤顶,新手司机看过来,老司机教你正确用法...
  2. 摄像头成像数据可见窗口计算
  3. 虚拟机如何设置ftp服务器,虚拟机设置ftp服务器配置
  4. 深入理解java虚拟机 - jvm高级特性与最佳实践(第三版)_深入理解Java虚拟机,JVM高级特性与最佳实践!...
  5. ik分词器 分词原理_ElasticSearch 集成Ik分词器
  6. 【Flink】Flink ChildFirstClassLoader loadClassWithoutExceptionHandling 空指针
  7. 95-35-010-Topic-Topic的新建 源码解析
  8. 【Flink】数据传输 挖个坑 把自己埋了 ClassCastException String cannot be cast to [LJava.lang.String
  9. 【es】Elasticsearch:inverted index,doc_values及source
  10. 95-150-045-源码-Sink-Streaming Parquet File