PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)
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 (.
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
题意:
给一个数组n,先对各个数字位从大到小排序得到a,然后逆置得到b,计算a-b,得到c,重复上述过程,直到得到6174,或者,对于特例,四个数字位完全相同,那么输出0000。
题解:
刚开始输入的数字可能不足四位要自动补0 以后的结果可能也不足四位都要自动补0
结果是0或者6174都是要退出的
一开始没考虑到6174,测试点5没过,后来想到了
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[];
int main(){
cin>>n;
int big=;
int small=;
int x=n;
int k=,f;
if(x==){
printf("7641 - 1467 = 6174");
return ;
}
while(x!=){
k=;
big=;
small=;
while(x>=){
a[++k]=x%;
x/=;
}
a[++k]=x;
while(k<){
a[++k]=;
}
sort(a+,a++);
for(int i=;i<=;i++){
big=big*+a[-i];
small=small*+a[i];
}
x=big-small;
printf("%04d - %04d = %04d\n",big,small,x);
if(x==) break;
}
return ;
}
更简洁的string处理的代码:
#include<bits/stdc++.h>
using namespace std;
bool cmp(char a,char b)
{
return a>b;
}
int main(void)
{
string s;
cin>>s;
s.insert(,-s.size(),'');
do{
string a=s,b=s;
sort(a.begin(),a.end(),cmp);
sort(b.begin(),b.end());
int diff=stoi(a)-stoi(b);
s=to_string(diff);
s.insert(,-s.size(),'');
cout<<a<<" - "<<b<<" = "<<s<<endl;
}while(s!=""&&s!="");
return ; ————————————————
版权声明:本文为CSDN博主「Imagirl1」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Imagirl1/article/details/82261213
PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)的更多相关文章
- 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 分)
题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...
- 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甲级】1023 Have Fun with Numbers (20 分)
题意: 输入一个不超过20位的正整数,问乘2以后是否和之前的数组排列相同(数字种类和出现的个数不变),输出Yes或No,并输出乘2后的数字. AAAAAccepted code: #define HA ...
- 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甲级:1124 Raffle for Weibo Followers (20分)
PAT甲级:1124 Raffle for Weibo Followers (20分) 题干 John got a full mark on PAT. He was so happy that he ...
- 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 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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- Django上传文件和修改date格式
上传大文件的时候: 修改date数据:
- Android平台5+ API提前生效,支持在plusready事件前调用
ios上plus是一直存在的,不涉及等ready事件.但安卓上还是需要等plus ready.在安卓环境中,通常情况下需要html页面解析完成后才会让5+ API生效,安卓的执行的顺序为: 加载htm ...
- httpclient: 设置连接池及超时配置,请求数据:PoolingHttpClientConnectionManager
public static void main(String[] args) throws Exception{ //httpclient连接池 //创建连接池 PoolingHttpClientCo ...
- react 面试指南
------------恢复内容开始------------ 什么是声明式编程 声明式编程是一种编程范式,它关注的是你要做什么,而不是如何做.它表达逻辑而不显式地定义步骤.这意味着我们需要根据逻辑的计 ...
- asp.net之大文件断点续传
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
- learning java AWT BoxLayout布局管理器
import javax.swing.*; import java.awt.*; public class BoxSpaceTest { private Frame f = new Frame(&qu ...
- learning java Math类
output: //取整,返回小于目标数的最大整数System.out.println(Math.floor(-1.2));// 取整,返回在于目标数的最大整数System.out.println(M ...
- samba 配置参数详解
samba 配置参数详解: 一.全局配置参数 workgroup = WORKGROUP说明:设定 Samba Server 所要加入的工作组或者域. server string = Samba S ...
- 浅谈sharding jdbc
定位为轻量级Java框架,在Java的JDBC层提供的额外服务. 它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架. ...
- tecplot-计算合速度的一种方法
原视频下载地址: http://yunpan.cn/cudFwWr8tFsxV 访问密码 75a8