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<iostream>
#include<sstream>
#include<math.h>
#include<iomanip>
using namespace std;
void Insertion_sort(int a[],int N){
int i,j;
for(i=1;i<N;i++){
int temp=a[i];
for(j=i;j>0;j--)
if(a[j-1]>temp) swap(a[j-1],a[j]);
else break;
a[j]=temp;
}
}
int main(){
string s;
cin>>s;
s.insert(0,4-s.length(),'0');
int a[4];
int r=0;
while(r!=6174){
int m=0,n=0;
for(int i=0;i<4;i++)
a[i]=s[i]-'0';
Insertion_sort(a,4);
for(int i=0;i<4;i++){
m+=a[i]*pow(10,i);
n+=a[i]*pow(10,3-i);
}
r=m-n;
cout<<setw(4)<<setfill('0')<<m;
cout<<" - "; cout<<setw(4)<<setfill('0')<<n;
cout<<" = "; cout<<setw(4)<<setfill('0')<<r<<endl;
if(r==0) break;
ostringstream os;
os<<setw(4)<<setfill('0')<<r;
s=os.str();
}
return 0;
}

1069. The Black Hole of Numbers的更多相关文章

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

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

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

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

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

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

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

  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. POJ 2891 Strange Way to Express Integers 中国剩余定理解法

    一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...

  2. 熊猫猪新系统測试之中的一个:Windows 10 技术预览版

    话说本猫不用windows非常多年了呀! 只是看到微软最新的Windows10还是手痒了.想安装体验一把. 于是第一时间下载,并做成usb引导安装镜像,在08年的老台式机上安装尝鲜鸟.下载ISO和安装 ...

  3. SIPp web frontend(2)

    SIP VoIP 測试交流群: 323827101 欢迎大家转载.为保留作者成果,转载请注明出处.http://blog.csdn.net/netluoriver,有些文件在资源中也能够下载.假设你没 ...

  4. 1366 xth 的第 12 枚硬币

    1366 xth 的第 12 枚硬币  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 传说 xth 曾 ...

  5. 微信小程序初探(一、简单的数据请求)

    微信小程序出来有一段时间了,之前没看好小程序(觉得小程序体验不咋好,内心对新事物有抵触心里,请原谅我的肤浅[捂脸][捂脸]),不过后来偶然之间玩过小程序的游戏(跳一跳.球球大作战.猜画小歌 等),顿悟 ...

  6. Elias-Fano编码算法——倒排索引压缩用,本质上就是桶排序数据结构思路

    Elias-Fano编码过程如下:把一组整数的最低l位连接在一起,同时把高位以严格单调增的排序划分为桶. Example: 2, 3, 5, 7, 11, 13, 24 Count in unary ...

  7. POJ 3579 Median (二分)

                                                                                                         ...

  8. Python 30 单例模式

    单例模式 多次实例化的结果指向同一个实例 单例模式实现方式 import settings #方式一: class MySQL: __instance=None def __init__(self,i ...

  9. BZOJ 4332 FFT+快速幂

    思路: 最裸的方程:f[i][j]=Σf[i-1][j-k]*F[k] 诶呦 这不是卷积嘛 f[i]就可以用f[i-1]卷F 求到 但是这样还是很慢 设p[i] 为Σ f[j](1<=j< ...

  10. 最小生成树之Prim算法(最原始最详细入门)

    //算法6.8 普里姆算法 #include <iostream> using namespace std; typedef char VerTexType; typedef int Ar ...