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、数字都要四位的2、如果是判断下一个式子的差不等于上个结果,然后跳出的话,那么需要判断,是不是第一次输出。比如,输入6174,结果就等于6174,那么就会没输出,直接跳出。

 #include <iostream>

 #include <algorithm>

 #include<string>

 #include <sstream>

 #include <iomanip>

 using namespace std;

 int a1[];

 int a2[];

 int bb[];

 bool cmp1(int a,int b)

 {

    return a>b;

 }

 bool cmp2(int a,int b)

 {

    return a<b;

 }

 int main()

 {

       string n;

       int i;

       while(cin>>n)

       {

          int tt,c1,c2;

          stringstream ss1;

          ss1<<n;

          ss1>>tt;

          i=;

          bool fir=true;

          while(true)

          {

                   string ss;

                   stringstream ss2;

                   ss2<<setfill('')<<setw()<<tt;

                   ss2>>ss;

                   for(i=;i<ss.length();i++)

                   {

                  a1[i]=ss[i]-'';

                    a2[i]=a1[i];

                   }

              sort(a1,a1+ss.length(),cmp1);

                sort(a2,a2+ss.length(),cmp2);

                    c1=; c2=;

                for(i=;i<ss.length();i++)

                   {

                    c1=c1*+a1[i];

                      c2=c2*+a2[i];

                   }

                   if(c1-c2==tt&&!fir)  break;

                   else 

                   {

                         fir=false;

                         cout<<setfill('')<<setw()<<c1<<" - "<<setfill('')<<setw()<<c2<<" = "<<setfill('')<<setw()<<c1-c2<<endl;

                         tt=c1-c2;

                   }     

          }  

       }

    return ;

 }

The Black Hole of Numbers (strtoint+inttostr+sort)的更多相关文章

  1. PAT 1069 The Black Hole of Numbers

    1069 The Black Hole of Numbers (20 分)   For any 4-digit integer except the ones with all the digits ...

  2. PAT 1069 The Black Hole of Numbers[简单]

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...

  3. pat1069. The Black Hole of Numbers (20)

    1069. The Black Hole of Numbers (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...

  4. 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 ...

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

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...

  6. 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 ...

  7. PAT_A1069#The Black Hole of Numbers

    Source: PAT A1069 The Black Hole of Numbers (20 分) Description: For any 4-digit integer except the o ...

  8. 1069 The Black Hole of Numbers (20分)

    1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...

  9. 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 ...

随机推荐

  1. 浅谈JS执行环境及作用域

     今天刚刚开通博客,也是第一次写博文,略感紧张.作为一个表达能力弱弱的人来说,自己花三分钟理解一个知识点,当别人问起时,也许需要30分钟才只是让别人知道自己在说什么,一点也不夸张,希望在博客上可以练习 ...

  2. uva 133 The Dole Queue 双向约瑟夫环 模拟实现

    双向约瑟夫环. 数据规模只有20,模拟掉了.(其实公式我还是不太会推,有空得看看) 值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉. 还有输出也很坑爹! 在这里不得不抱怨下Uva的o ...

  3. 3.4.2内核下的I2C驱动

    1. 框架1.1 硬件协议简介1.2 驱动框架1.3 bus-drv-dev模型及写程序a. 设备的4种构建方法a.1 定义一个i2c_board_info, 里面有:名字, 设备地址 然后i2c_r ...

  4. 给jdk写注释系列之jdk1.6容器(9)-Strategy设计模式之Comparable&Comparator接口

    前面我们说TreeMap和TreeSet都是有顺序的集合,而顺序的维持是要靠一个比较器Comparator或者map的key实现Comparable接口.      既然说到排序,首先我们不用去关心什 ...

  5. 怎么删除远程登录连接的ip

    通过远程桌面可以登录到远程电脑上进行相应的操作,在登录过后会在本地电脑上留下登录过的IP以及登录用户名相关信息,可能会给远程的电脑带来安全隐患,下面介绍一下清除远程桌面历史记录的方法. 1.删除我的文 ...

  6. CF Drazil and His Happy Friends

    Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. HDU 1160 FatMouse's Speed (DP)

    FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  8. hdu 4612 边连通度缩点+树的最长路径

    思路:将以桥为分界的所有连通分支进行缩点,得到一颗树,求出树的直径.再用树上的点减去直径,再减一 #pragma comment(linker, "/STACK:1024000000,102 ...

  9. C# 按指定数量从前面或者后面删除字符串

    为了方便处理一些数据,自己写一个小程序来辅助工作,提高点效率.  不够删除怎么办呢!?一般程序员都会马上想到的是,用if判断…….这里其实不用if语句也可以轻松实现的! 有些程序员还可能用 try c ...

  10. HTTP - 持久连接

    Web 客户端经常会打开到同一个站点的连接.比如,一个 Web 页面上的大部分内嵌图片通常都是来自同一个 Web 站点,而且相当一部分指向其他对象的超链接通常都指向同一个站点.因此,初始化了对某服务器 ...