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. IOS 中微信 网页授权报 key[也就是code]失效 解决办法

    枪魂微信平台ios手机点击返回 网页授权失败,报key失效.已经解决,原因是授权key只能使用一次,再次使用就会失效. 解决办法:第一次从菜单中进行授权时,用session记录key和open_id. ...

  2. 多媒体开发之分场图像和交错图像interlacing---一个破解版的迅雷云点播网站

    [-] 目录 编辑描述 编辑去交错方法 编辑去交错源自电影的影像 编辑去交错交错式影像 编辑单一场去交错intra-field deinterlacing 编辑场间去交错inter-field dei ...

  3. 线程本地变更,即ThreadLocal-->Spring事务管理

    我们知道Spring通过各种模板类降低了开发者使用各种数据持久技术的难度.这些模板类都是线程安全的,也就是说,多个DAO可以复用同一个模板实例而不会发生冲突.我们使用模板类访问底层数据,根据持久化技术 ...

  4. WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法

    WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法 在WPF的TextBox的LostFocus事件中直接使用Focus()方法会出现死循环的问题 正确的使用方式有2中方法: 方法一 ...

  5. 用公式编辑器编辑n元乘积的方法

    在数学中经常会出现很多个元素进行求和或者是乘积的情况,但是在整个数学过程中,不可能将所有的元素都写出来,这样很费时费力同时过程也很赘余,不能很好地理解其中的过程,因此数学中对于这一类的多元相加或者相乘 ...

  6. Hadoop1.x目录结构及Eclipse导入Hadoop源码项目

    这是解压hadoop后,hadoop-1.2.1目录 各目录结构及说明: Eclipse导入Hadoop源码项目: 注意:如果没有ant的包可以去网上下,不是hadoop里面的. 然后如果通过以上还报 ...

  7. MVC模式 与 Model2模型 介绍

    Model1回顾 MVC模式:MVC(Model.View.Controller)是软件开发过程中比较流行的设计思想.旨在分离模型.控制.师徒.是一种分层思想的体现. Model2简介Java Web ...

  8. Windows下IPython安装

    1:安装Python, 下载后安装即可:https://www.python.org/downloads/windows/,(选择Python2或Python3) 添加Path环境变量 2:安装ez_ ...

  9. 【黑金原创教程】【TimeQuest】【第一章】TimeQuest 静态时序分析模型的概念

      声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/ ...

  10. Tomcat上进行项目部署与WAR包含义

    一.WAR包 1.与JAR包类似,是将java类(编译好的.class)和Web资源,压缩后生成压缩包. 2. 与JAR相比,多了WEB-INF文件夹,其它多出来的内容基本都是Web资源 WEB-IN ...