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 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 (0,10​4​​).

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
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e5 + ; int n, A[], ans_min, ans_max, cnt; int my_pow(int x, int n)
{
int ans = ;
while (n)
{
if (n & ) ans *= x;
x *= x;
n >>= ;
}
return ans;
} int main()
{
// freopen("Date1.txt", "r", stdin);
scanf("%d", &n);
if (n == )
{
printf("7641 - 1467 = 6174\n");
return ;
}
while ()
{
if (n == ) return ;
ans_max = ans_min = cnt = ;
while (n)
{
A[cnt ++] = n % ;
n /= ;
}
sort(A, A + , less<int>());
for (int i = ; i < ; ++ i)
ans_max += A[i] * my_pow(, i);
sort(A, A + , greater<int>());
for (int i = ; i < ; ++ i)
ans_min += A[i] * my_pow(, i); if (ans_min == ans_max)
{
printf("%04d - %04d = 0000\n", ans_max, ans_min);
return ;
}
n = ans_max - ans_min;
printf("%04d - %04d = %04d\n", ans_max, ans_min, n);
}
return ;
}

pat 1069 The Black Hole of Numbers(20 分)的更多相关文章

  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. 1069 The Black Hole of Numbers (20分)

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

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

  4. 【PAT甲级】1069 The Black Hole of Numbers (20 分)

    题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...

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

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

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

  8. PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)

    1023 Have Fun with Numbers (20 分)   Notice that the number 123456789 is a 9-digit number consisting ...

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

随机推荐

  1. [Luogu3069][USACO13JAN]牛的阵容Cow Lineup

    题目描述 Farmer John's N cows (1 <= N <= 100,000) are lined up in a row. Each cow is identified by ...

  2. ArraryList源码解读

    先由一道题引发思考: ArrayList list = new ArrayList(20);中的list扩充几次() A 0     B 1     C 2      D 3 答案:A 直接翻看 jd ...

  3. 安装并使用SourceTree进行代码管理(Mac环境)

    应用场景 对于我们开发人员来说,熟练使用Git是最基本的技能之一.SourceTree又是一款比较好的Git UI工具,是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,主要 ...

  4. golang会取代php吗

    看看PHP和Golang如何在开发速度,性能,安全性,可伸缩性等方面展开合作. PHP与Golang比较是一个艰难的比较. PHP最初创建于1994年,已有24年.自那时起,由于PHP的开源格式,易用 ...

  5. 百万年薪python之路 -- python2和python3的区别

    python2和python3的区别: python2获取的是整数 python3获取的是浮点数 print函数:(Python3中print为一个函数,必须用括号括起来:Python2中print为 ...

  6. linq一般用法

    最一般的用法 var rows = from c in dataTrue.AsEnumerable() from t in dataPre.AsEnumerable() ].ToString().St ...

  7. 解决 IDEA 创建 Gradle 项目没有src目录

    第一次写博客,前几天遇到一个问题,就是使用ider创建gradle项目后,src目录没有自动生成出来,今天就给大家分享一下怎么解决. 目录: 1.创建Gradle项目 2.解决没有生成src目录问题 ...

  8. PHP Windows下使用Memcached扩展

    github上发现了一个很好用的小文件,能够 模拟出php_memcached.dll的PHP memcached扩展. PHP memcached client 已知PCEL有两个memcache ...

  9. Java基础(三十)泛型程序(Generic Programming)

    一.泛型程序的定义和使用 1.为什么要使用泛型程序设计 泛型程序设计意味着编写的代码可以被很多不同类型的对象所重用.同时,使得程序具有更好的可读性和安全性. ArrayList<String&g ...

  10. Ios 开发 mac cocoaPods的环境搭建

    CocoaPods不多介绍,一个大家几乎都会使用的第三方库的管理框架! 本文主要介绍如何安装和使用CocoaPods,本人亲测可行. 1.Ruby环境搭建 查看下当前ruby版本: ruby -v 更 ...