1069. The Black Hole of Numbers
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 1069 The Black Hole of Numbers (20分)
1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- MySql 同一个列中的内容进行批量改动
问题重现: MySql 数据库中,一给列的内容中包含 ".wmv" 须要将 "." 后的wmv格式 换为"flv" 解决的方法 up ...
- CH Round #24 - 三体杯 Round #1-C 逃不掉的路
留个e-DCC的板子 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...
- golomb哥伦布编码——本质上就是通过0来区分商和余数
哥伦布编码是一个针对整数的变长编码方式,详细介绍可以看维基百科.这里简单介绍下: 哥伦布编码使用指定的整数 M 把输入的整数分成两部分:商数 q.余数 r. 商数当做一元编码,而余数放在后面做为可缩短 ...
- 【HDU 6162】 Ch’s gift
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6162 [算法] 离线树剖 我们知道,u到v路径上权值为[A,B]的数的和 = u到v路径上权值小于 ...
- bzoj2595 [Wc2008]游览计划——斯坦纳树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2595 今天刚学了斯坦纳树,还不太会,写一道题练习一下: 参考了博客:http://www.c ...
- php 图片生成器
一.需求 最近公司由于有大量的海报要做,而且海报的布局规模都是一样的,只是内容不同,所以老板想我开发一个图片的生成器.可以根据你输入的内容生成海报图片. 具体有需求有以下的需求 1.可以根据将每条数据 ...
- RabbitMQ 官方NET教程(六)【RPC】
在第二个教程中,我们学习了如何使用Work Queues在多个工作者之间分配耗时的任务. 但是如果我们需要在远程计算机上运行功能并等待结果怎么办? 那是一个不同的模式. 此模式通常称为远程过程调用或R ...
- OI知识点
- javascript中计算点击多少次
点击事件:onclick <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- 关于基础的Set 和Get
先附上一篇文章,讲的很清楚 在Core中,我们要是先这样设置了.在我们对这个上下文做查询工作的时候,例如: var head = _OMSECDatabase.OmsEcorderHead.Where ...