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,10​4​​).

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

题目大意:给定一个四位数,展示出每位数从大到小排列,与从小到大排列的差值,直到出现6174黑洞数停止。

//这道题目是简单的,但是还是遇见了一些问题:

AC:

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdlib.h>
#include<cstdio>
using namespace std; int main()
{
int n;
cin>>n;
int a[],big,small,res=-;
int temp=;
while(res!=)
{
fill(a,a+,);
while(n!=)
{
a[temp++]=n%;
n/=;
//cout<<"kk";
}
temp=;
sort(a,a+);//默认从小到大排列
small=a[]*+a[]*+a[]*+a[];
big=a[]*+a[]*+a[]*+a[];
if(a[]==a[]&&a[]==a[]&&a[]==a[])
{
printf("%04d - %04d = 0000\n",big,small);
return ;
}
res=big-small;
printf("%04d - %04d = %04d\n",big,small,res);
n=res;
}
return ;
}

1.第一次忽略了temp=0;应该在使用过后将其赋值为0的;

2. 应该将a数组初始化为0,要不然下次会有影响的,比如在有0位的时候:

3.最后就是提交发现第0个测试点过不去,原来是因为相同数的时候只输出了0,而不是0000!

PAT 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(20 分)

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...

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

  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 分)(内含别人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 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 ...

  8. PAT (Advanced Level) 1069. The Black Hole of Numbers (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  9. PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. kettle的日志

    http://blog.sina.com.cn/s/blog_76a8411a01010u2h.html

  2. 2015 Multi-University Training Contest 3 1002 RGCDQ

    RGCDQ Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5317 Mean: 定义函数f(x)表示:x的不同素因子个数. 如:f ...

  3. hive组件和执行过程

    转自http://blog.csdn.net/lifuxiangcaohui/article/details/40262021 对Hive的基本组成进行了总结: 1.组件: 元存储(Metastore ...

  4. 硬件设计之串口收发器---ISO1050 (现行) 隔离式 5V CAN 收发器

    http://www.ti.com.cn/product/cn/iso1050 http://www.deyisupport.com/question_answer/dsp_arm/sitara_ar ...

  5. mysql关联取附表最后一条记录,附加lareval orm实现

    MySQL 多表关联一对多查询取最新的一条数据:https://blog.csdn.net/u013902368/article/details/86615382 Laravel query buil ...

  6. ielowutil.exe应用程序错误解决方法

    转载: http://wenda.so.com/q/1484111785202192 控制台方法: .按住“Window”+“R”->输入“cmd”->确定 .输入“ ”->回车-& ...

  7. asynDBCenter(不断跟新)

    GameServer以前访问DBcenter时同步的,这样服务器都要等待DBcenter返回结果,经理在DBcenter和GameServer之间加了一个asynDBCenter,就实现了异步,感觉还 ...

  8. sessionStorage存储json对象

    应用场景: 账单列表中A页面:点击其中的一列,ajax返回的数据在这一页 点击进入账单详情B页面: 因为在A页面已经做过ajax的请求了,所以希望把当前其中的一个数组对象传到B页面中,所以,就考虑到暂 ...

  9. eclipse 的代码着色插件 --Eclipse Color Theme

    Eclipse Color ThemeEclipse自带的背景颜色是白色的,很伤眼睛,故而安装一个颜色和主题插件,来改变代码区域的背景颜色以及关键字的颜色. 网址:http://eclipsecolo ...

  10. Android XListView下拉刷新、上拉载入更多

    source code: https://github.com/Maxwin-z/XListView-Android 提供了两个接口: a) IXListViewListener:  触发下拉刷新.上 ...