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 ...
随机推荐
- Runnable、Callable、Future、FutureTask的区别
转自:https://blog.csdn.net/jdsjlzx/article/details/52912701 FutureTask既是Future.Runnable,又是包装了Callable( ...
- Web Service-第一篇什么是Web Service
1.什么是WebService? WebService是一种跨编程语言和跨操作系统平台的远程调用技术.WebService就是一个应用程序向外界暴露出一个能通过Web进行调用的API,也就是说能用编程 ...
- java_第一年_JDBC(3)
事务 我们在通过JDBC连接数据库并开始交互时,默认情况下是自动提交的,有时由于为了保持业务流程的完整性.提高性能或是使用分布式事务,需要启动支持事务,此时的方法是调用Connection对象的set ...
- 二叉树BinTree类定义
#include<iostream> using namespace std; template<class T> struct BinTreeNode{//二叉树结点类 T ...
- luogu P3657 (NOIP2017) 跳房子(二分+DP+单调队列)
题面 传送门 分析 显然答案有单调性,可以二分答案,设当前二分值为g,根据题意我们可以求出跳跃长度的范围[l,r] 考虑DP 子状态: dp[i]表示跳到第i个点时的最大和 状态转移方程 \(dp[i ...
- c#catch循环内捕获到异常继续循环
一,如果我们将异常而不影响循环,如下代码: using System; using System.Collections.Generic; using System.Linq; using Syste ...
- 【学习总结】gcc和gdb
目录 <> vim.gcc.gdb: gcc: gcc和g++是c/c++的linux系统集成的编译器,源文件的后缀应为 .C/.cpp/.c++/.cc等 编译器可以将C.C++等语言源 ...
- JavaScript中的map()函数
概述Array.map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,同时不会改变原来的数组. 用法 Array.map(callback); 示例 //简单数组 const ...
- H5中滚动到底部的事件
问题:在H5中,我们有这样的需求:例如有列表的时候,滚动到底部时,需要加载更多. 解决方案:可以采用window的滚动事件进行处理 分析:如果滚动是针对整个屏幕而言的(不针对于某个界面小块),那么这个 ...
- samba - 为 UNIX 实现的 Windows SMB/CIFS 文件服务器
SYNOPSIS 总览 Samba DESCRIPTION 描述 samba 套件是在 UNIX 系统上实现“服务器信息块”(通常简称 SMB) 协议的一组程序.这个协议有时也称为“通用互联网文件系统 ...