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

注意点:
(1)题目并未保证输入的n一定是在[1000,10000)之间的数,所以对于输入为53这样的数,第一行输出应为5300 - 0035 = 5265

(2)当输入的数为0或者6174时要特殊判断,输入0输出应为0000 - 0000 = 0000;输入6174输出应为7641 - 1467= 6174,而不能没有输出

(3)输出的数必须为4位,不够4位要在高位补0


 #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int A, B, preAns = -, Ans = , flag = ;
string Na, Nb;
cin >> Ans;
while (preAns != Ans)
{
preAns = Ans;
Na = to_string(Ans);
while (Na.length() < )
Na += "";
sort(Na.begin(), Na.end(), [](char a, char b) {return a > b; });
A = stoi(Na.c_str());
sort(Na.begin(), Na.end(), [](char a, char b) {return a < b; });
B = stoi(Na.c_str());
Ans = A - B;
if (flag == && preAns == Ans)
break;
printf("%04d - %04d = %04d\n", A, B, Ans);
flag = ;//怎么都得有一行输出
}
return ;
}

APT甲级——A1069 The Black Hole of Numbers的更多相关文章

  1. 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 ...

  2. A1069. The Black Hole of Numbers

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  3. PAT_A1069#The Black Hole of Numbers

    Source: PAT A1069 The Black Hole of Numbers (20 分) Description: For any 4-digit integer except the o ...

  4. 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 ...

  5. 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 ...

  6. pat1069. The Black Hole of Numbers (20)

    1069. The Black Hole of Numbers (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...

  7. 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 ...

  8. 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 ...

  9. 1069 The Black Hole of Numbers (20分)

    1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...

随机推荐

  1. Entity Framework Code First使用者的福音 --- EF Power Tool使用记之二(问题探究)

    转:http://www.cnblogs.com/LingzhiSun/archive/2011/06/13/EFPowerTool_2.html   上次为大家介绍EF Power Tool之后,不 ...

  2. c#上传下载ftp(支持断点续传)

    using System;using System.Net;using System.IO;using System.Text;using System.Net.Sockets;namespace f ...

  3. .NETFramework:template

    ylbtech-.NETFramework: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://y ...

  4. node.js在ubuntu上和windows上的安装

    Ubuntu 上安装 Node.js Node.js 源码安装 以下部分我们将介绍在Ubuntu Linux下安装 Node.js . 其他的Linux系统,如Centos等类似如下安装步骤. 在 G ...

  5. idea从github中pull或者push成功之后ssm项目全部controller报红色下划线的解决方案

    某次从github上pull下来之后,报出如下一堆红色波浪线错误 解决方法: 在随便一个红色波浪线处,按下alt+enter,点击add maven dependency, 选中所有,点击添加即可,此 ...

  6. IMS Call中的SS

    1Hold procedure:对于每一个被HOLD的媒体流,SDP包含: 如果流之前被设置为“recvonly”媒体流则是一个“不活动”的SDP属性: 如果先前将流设置为“sendrecv”媒体流则 ...

  7. java-day08

    继承概念 继承是多态的前提,主要用于解决共性抽取 特点 子类可以拥有父类的内容,子类也可以有自己的专属内容 格式 public class 父类{} public class 子类 extends 父 ...

  8. DNS配置-BIND安装配置全过程

    下载地址:ftp://ftp.isc.org/isc/ 下载bind,我下载的是bind-9.11.13.tar.gz 我下载的文件放在/root目录下进入目录解压缩 [root@localhost ...

  9. html--div里让图片水平居中

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Windows相关命令

    1.查看端口8080被哪个进程占用 netstat -ano | findstr "8080" 2.查看进程号为5768对应的进程 tasklist | findstr " ...