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 ...
随机推荐
- 基于Windows Azure 安装 SharePoint 2010简体中文语言包
在Windows Azure上安装的Windows Server默认是英文版本的,当时安装的SharePoint也是英文版的,为方便使用,决定安装中文的语言包,具体过程如下: 1. 安装 Window ...
- kendo AutoComplete实现多筛选条件
kendo autoComplete 原始情况下是不支持多筛选条件的 $("#autocomplete").kendoAutoComplete({ filter: "co ...
- vbs socket
http://www.bathome.net/thread-423-1-1.html http://files.cnblogs.com/files/developer-ios/mswinsck.ocx ...
- CocoaPods建立私有仓库
项目管理:CocoaPods建立私有仓库 2015-05-08 10:22 编辑: lansekuangtu 分类:iOS开发 来源:agdsdl 0 6367 CocoaPods项目管理私有仓库 招 ...
- 冒泡排序Vs直接选择排序
什么是排序?为什么要使用排序?事实上我们生活中处处都用到了排序.拿字典来说,如今,我们要在字典中查找某个字(已经知道这个字的读音),首先.我们须要依据这个字的读音,找到它所所在文件夹中的位置,然后依据 ...
- acdream 1414 Geometry Problem
Geometry Problem Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) ...
- Linux下,安装配置Weblogic
环境说明 系统 -- Linux RHEL5 32bit 环境 -- 局域网中在192.168.0.140(windows)通过xshell连接服务器 软件 -- 1.JDK:1.5.0_15 2. ...
- 二维矩阵相乘 in C++
#include <iostream> #include <vector> #include <string> #include <sstream> # ...
- [SpringMVC]定义多个前缀映射的问题
转自:https://penciltim.iteye.com/blog/501073 我在web.xml里面定义多个dispatch-servlet的前缀映射,像下面这样 <!-- Servle ...
- 判断ascii码是什么的函数
function CharMode(iN){ if (iN>=48 && iN <=57) //数字 return 1; if (iN>=65 && ...