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 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"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"algorithm"
using namespace std;
bool compareChar(char c1, char c2){
if(c1<c2)
return false;
return true;
}
char * stringMinus(char *s1, char *s2){
for(int i=3;i>=0;i--){
if(s1[i]>=s2[i]){
s1[i] = '0'+(s1[i]-s2[i]);
}
else{
s1[i] = '0'+(s1[i]-s2[i]+10);
if(i>0)
s1[i-1] -= 1;
}
}
return s1;
} int main()
{
int n;
char s[5]="0000";
char incr[5];
scanf("%d",&n);
int i=0;
while(n){
int x = n%10;
s[3-i] = '0'+x;
n/=10;
i++;
}
while(strcmp(s,"0000")!=0){
sort(s,s+4);
strcpy(incr,s); sort(s,s+4,compareChar); printf("%s - %s = ",s,incr);
printf("%s\n",stringMinus(s,incr));
if(strcmp(s,"6174")==0){
break;
}
}
return 0;
}
1069. The Black Hole of Numbers (20)的更多相关文章
- 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 分)(内含别人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 ...
- PAT Advanced 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 ...
- PAT (Advanced Level) 1069. The Black Hole of Numbers (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【PAT甲级】1069 The Black Hole of Numbers (20 分)
题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...
- 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 ...
随机推荐
- Blog Starting...
30出头,开始Blog记录学习生活的点滴,待40时再回来一看.
- Android学习系列(40)--Android主题和样式之系统篇(下)
11)Widget样式(Widget Style) 特别说明,此处定义大量的系统内置控件的样式,对于重写原生控件的样式具有很大的参考价值. <!-- Widget styles --> & ...
- POJ 1635 树的最小表示法/HASH
题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...
- no-jquery 04 Events
Events Sending Native (DOM) Events anchorElement.click(); Sending Custom Events var event = document ...
- 解决Myeclipse10 Building Workspace速度慢的问题
解决方法如下: 选择项目,选择Project -> Properties -> Builders,取消JavaScript Validator,validation或者其它你认为没必要的选 ...
- 解决js(ajax)提交后端的“ _xsrf' argument missing from POST” 的错误
首先先简述一下CSRF: CSRF是Cross Site Request Forgery的缩写(也缩写为XSRF),直译过来就是跨站请求伪造的意思,也就是在用户会话下对某个CGI做一些GET/POST ...
- JS(ajax笔记)
简介:AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法,是与服务器交 ...
- java基础-数组
浏览以下内容前,请点击并阅读 声明 定义:数组是一个能容纳固定数量,类型单一的若干个值的容器.注意,数组是一个对象. 数组一旦创建,则其长度固定不变,数组中的所有值叫元素(Element),获取元素要 ...
- CF460 A. Vasya and Socks
A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Ferry Loading III[HDU1146]
Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...