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 until6174
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
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 ...
随机推荐
- python 谈赋值和copy区别
1变量赋值 a=1 b=a b=1 a=3 b还是等于1 2,列表赋值 嵌套列表情况 如果想让列表单独出来怎么办呢 ,这时候用到copy 如果列表嵌套列表情况 还是一样的, 如果想深度copy imp ...
- 【学习总结】尚硅谷2019java数据结构和算法
相关链接 github:javaDSA 目录 第一章 内容介绍和授课方式 第二章 数据结构和算法概述 第三章 稀疏数组和队列 第四章 链表 第五章 栈 第六章 递归 第七章 排序算法 第八章 查找算法 ...
- 【JAVA】 03-Java中的异常和包的使用
链接: 笔记目录:毕向东Java基础视频教程-笔记 GitHub库:JavaBXD33 目录: <> <> 内容待整理: 异常 异常和错误的发生和区别 异常:java运行期间发 ...
- 关于Object.create方法
ES6最新的Object.create语法是 创造一个对象 可以传参,参数为一个对象,得到的结果是一个克隆的对象, 实际上 这是基于原型的克隆 分析如下: var a={b:1}; var a1 = ...
- canvas 画正方形和圆形
绘制正方形 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Django组件——用户认证
用户认证 一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1 .authentica ...
- openssl版本升级
1.查看openssl版本 [root@node2 openssh-8.0p1]# openssl version OpenSSL 1.0.1e-fips 11 Feb 2013 2.下载指定版本的o ...
- runltp出现问题 [
runltp 623行: if [ "$?" == "0" ]; then 对[解析出了问题. 我灵机一动,是不是sh的问题. which sh /bin/sh ...
- OpenCV图像数据字节对齐
目录 1. IplImage的data字段,是char*类型,是4字节对齐. 2. 手动创建的Mat通常是没有字节对齐的 3. 从IplImage转过来的Mat,是字节对齐的 4. 总结 图像数据是否 ...
- CentOS下安装gdb的方法
https://blog.csdn.net/zlk252620068/article/details/79564944