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. Virtualbox - 共享文件夹

    在虚拟机中添加共享文件夹——比如主机中的/home/user/download,我想把download文件夹共享给虚拟机用. 步骤:1.在虚拟机中找个挂载的目录,比如 /mnt/d,没有就新建一个目录 ...

  2. 关于JAVA那点事---i++和++i

    对i++和++i 一直有点晕,今天专门抽空来研究相关的知识. 先从简单的说起. 有如下程序: int i=0; i=i++; system.out.print(i); 一眼望去这个结果显而易见是1,但 ...

  3. python(3)-集合

    集合就是把不同的元素组织在一起,但在集合中不允许有重复的元素. >>> a = set() #创建集合 >>> type(a) <class 'set'> ...

  4. linux安装

    1.rpm: • RPM是软件包管理工具,是Redhat Package Manager的缩写,最早由redhat公司引入的,现在已经成为公认的行业标准了.• 什么是package• 简单地说就是归档 ...

  5. LeetCode 61

    Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For exa ...

  6. 【基础数学知识】UVa 11314 - Hardly Hard

    Problem H HARDLY HARD You have been given the task of cutting out a quadrilateral slice of cake out ...

  7. JMS - 基本概念

    连接工厂创建连接对象的工厂. 连接客户端与 JMS 服务器之间建立的连接.创建一个或多个会话. 会话创建消息.生产者和消费者,会话是 消息由三部分组成:消息头.消息属性和消息体. 生产者创建和发送消息 ...

  8. SSIS_TXT有规则资料导入到EXCEL

    SSIS开发需要完全安装sqlserver.本次demo是sqlserver2008. 1.创建项目 2.解决方案打开如图所示. 3.拉取一个序列容器,一个数据流任务. 4.在数据流任务点击.拉取一个 ...

  9. python学习day2--python基础

    python没有常量的定义方式,一般常量可人为用全部大写字母来表示. 如: MYSQL_CONNECTION='192.168.10.2' 以os库为例: import os os.system('i ...

  10. 基于DIV+ul+li实现的表格(多示例)

    一个无序列表biaoge,前四个列表项我们赋予了类biaotou.因为这四个项是表格头部,应该与表格数据有所区别.所以单独赋予了类,可以方便控制.下面我们开始CSS代码的编写:由 www.169it. ...