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 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,104).
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[简单]的更多相关文章
- 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(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)
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)【模拟】——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 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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- KMP + 求最小循环节 --- HDU 1358 Period
Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...
- 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace
Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...
- MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用 ...
- thinkphp相关功能整合系列
thinkphp整合系列之短信验证码.订单通知 thinkphp整合系列之rbac的升级版auth权限管理系统demo thinkphp整合系列之阿里云oss thinkphp整合系列之phpmail ...
- hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)
二叉搜索树 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- iOS 功能代码 上传到 远程 码云私有库
推送代码到远程私有库 创建私有库(注意:仓库名称LYDKit必须和本地仓库对应的名称一样) 复制远程仓库的地址,打开终端,cd到对应的本地库路径下面 >>> cd /Users/cx ...
- a &a &a[0]之间的区别和联系
数组中,a为数组的首地址,&a[0]为数组第一个元素的地址. 所以 a == &a[0] 但是,&a又是什么东西呢? 我们来做下面的代码测试: #include <std ...
- Hadoop1.2.1 启停的Shell 脚本分析
停止shell脚本以此类推.
- Android "Please ensure that adb is correctly located at" 错误
转自:http://blog.csdn.net/hyx1990/article/details/12681207 遇到问题描述: 运行Android程序控制台输出 [2013-10-13 16:45: ...
- Eclipse配置Tomcat并运行
这篇文章介绍Eclipse配置tomcat.我们假设已经安装好JDK并且配置好了JDK的环境变量.然后我们需要下载并安装Eclipse和tomcat:Eclipse:http://www.eclips ...