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. ubuntu14.04中安装jdk

    1. 下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 将下载的 .g ...

  2. QQ在线聊天代码获取和使用教程

    在网站上挂上悬浮QQ是一种有效的推广方式,QQ正常情况下是不被允许临时会话的,需要加为好友才可以,这样很不友好, 当今每个行业都是有很多人在做,竞争很激烈,对客户的友好是增加订单的有效途径. 地址:h ...

  3. js 版本号

    在web项目开发过程中,我们经常会引用css.js文件,更新文件后常出现缓存问题(明明更改了代码,在浏览器上访问的时候却没有发生变化),这种情况我们通常采用以下两种解决方案: 1.手动清除浏览器缓存 ...

  4. CSS代码重构与优化

    CSS代码重构的基本方法 前面说到了CSS代码重构的目的,现在我们来说说一些如何达到这些目的的一些基本方法,这些方法都是易于理解,容易实施的一些手段,大家平时可能也不知不觉地在使用它. 提高CSS性能 ...

  5. C#中DllImport用法汇总

    最近使用DllImport,从网上google后发现,大部分内容都是相同,又从MSDN中搜集下,现将内容汇总,与大家分享. 大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比 ...

  6. 经验总结20--C#模拟WEB请求

    非常多语言能够使用代码进行WEB请求,获取到须要的数据. 方便调用别人的接口,自己进行处理. HttpWebRequest request = WebRequest.Create(url) as Ht ...

  7. Codeforces 417 D. Cunning Gena

    按monitor排序,然后状压DP... . D. Cunning Gena time limit per test 1 second memory limit per test 256 megaby ...

  8. TCP/IP 在 Windows 下的实现

    Windows 实现TCP/IP 协议也是建立在上一篇博客的OSI 基础之上的. 用户态是由ws2_32.dll 和一些其他服务提供者的 dll 共同实现,当中ws2_32.dll 是一个框架.能够容 ...

  9. css+jq写的小小的移动端按钮的动画改变(三个很闲变成一个叉号)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. win上gulp配置

    主线: 安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 1,安装node.js 1.1.说明 ...