PAT_A1069#The Black Hole of Numbers
Source:
Description:
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 until6174comes 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
Keys:
- 字符串处理
Attention:
- 早期的PAT考试更注重算法的效率(过于依赖容器会超时),而现在的PAT考试更注重解决问题的能力(强调容器的使用)
- to_string()会超时
Code:
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n1,n2,n;
vector<int> p();
scanf("%d",&n);
do
{
for(int i=; i<; i++)
{
p[i]=n%;
n/=;
}
sort(p.begin(),p.end(),less<char>());
n1 = p[]*+p[]*+p[]*+p[];
sort(p.begin(),p.end(),greater<char>());
n2 = p[]*+p[]*+p[]*+p[];
n = n2-n1;
printf("%04d - %04d = %04d\n", n2,n1,n);
}while(n!= && n!=); return ;
}
PAT_A1069#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[简单]
1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...
- pat1069. The Black Hole of Numbers (20)
1069. The Black Hole of Numbers (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...
- 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 分)
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 分)(内含别人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), ...
- 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 ...
- The Black Hole of Numbers (strtoint+inttostr+sort)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
随机推荐
- Linux-第一篇linux基本认识
1.在Linux世界中,一切皆是文件,Linux文件采用级层式的树状目录结构,在此结构中根目录是“/”. 一般linux系统的目录结构如下 目录结构说明 目录 说明 bin 存放二进制可执行文件(ls ...
- Trait这个类的特性
php从以前到现在一直都是单继承的语言,无法同时从两个基类中继承属性和方法,为了解决这个问题,php出了Trait这个特性 用法:通过在类中使用use 关键字,声明要组合的Trait名称,具体的Tra ...
- shell函数与位置参数举例
- ROLLBACK - 退出当前事务
SYNOPSIS ROLLBACK [ WORK | TRANSACTION ] DESCRIPTION 描述 ROLLBACK 回卷当前事务并取消当前事务中的所有更新. PARAMETERS 参数 ...
- MySQL数据库使用xtrabackup备份实现小例子
关于MySQL数据库的备份的工具和方式也比较多,本文只简单介绍一些我司一个平台的备份方案.Xtrabackup是由percona开源的免费数据库热备份软件,但是只能对InnoDB数据库和XtraDB存 ...
- HTML基础 img标签 做一个图库
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- CentOS-7 开放80端口
转载声明:本文转载 原文:https://blog.csdn.net/u013310075/article/details/80983117 关闭与开启防火墙 systemctl stop firew ...
- H2database创建表
语法和sql server大同小异 create table users(id int primary key not null int identity, name varchar(20))
- 关系型数据库---MYSQL---优化
1.为什么要进行SQL优化? 1.1 查询性能低 1.2 执行时间过长 1.3 等待时间过长 1.4 SQL写的太差(尤其是多表关联查询) 1.5 索引失效 1.6 服务器参数(缓存.线程数)设置不合 ...
- SQLServer 链接服务器及同义词
链接服务器 1. openrowse exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc ...