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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- C++处理异常
C++处理异常
- PHP项目部署 Linux 服务器
一.运行环境 Centos7 x64 lnmp (Linux , Nginx , Mysql , PHP/Python) 二.安装依赖和修改配置 安装Lnmp环境集成包:https://lnmp.or ...
- 入门node.js
我们现在要做一个简单的h5应用:包含登录.注册.修改密码.个人中心主页面.个人中心内页修改名称.个人中心修改手机号码. 第一步:工具安装,我选择了能够辅助我们快速开发的light开发工具 1. lig ...
- C#在WebApi 中使用Redis 的方法
首先Startup public void ConfigureServices(IServiceCollection services) { services.AddControllers(); // ...
- learning java AWT 手绘窗口
import java.awt.*;port java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import jav ...
- 关于Keil4 转到 Keil5以后的一些错误解决
一, 看自己选择CPU型号,根据型号再做配置 根据自己型号填写
- C二维字符数组的使用及如何获取二维数组的总行数和总列数!
#include <stdio.h> #include <stdlib.h> int main(){ char str[][30] = {"zhangsan" ...
- Codevs 1482 路线统计(矩阵乘法)
1482 路线统计 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description N个节点的有向图, 求从start到finish刚好经过时 ...
- CODE FESTIVAL 2017 qual C 题解
传送门 \(A\) 咕咕 const int N=15; char s[N];int n; int main(){ scanf("%s",s+1),n=strlen(s+1); f ...
- 《挑战30天C++入门极限》C++运算符重载函数基础及其值返回状态
C++运算符重载函数基础及其值返回状态 运算符重载是C++的重要组成部分,它可以让程序更加的简单易懂,简单的运算符使用可以使复杂函数的理解更直观. 对于普通对象来说我们很自然的会频繁使用算数运 ...