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, 10000).

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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp1(int a, int b){
return a < b;
}
bool cmp2(int a, int b){
return a > b;
}
void numSort(int n, int &r1, int &r2){
int temp[];
int i = ;
r1 = ; r2 = ;
do{
temp[i++] = n % ;
n = n / ;
}while(n != || i < );
sort(temp, temp + i, cmp1);
for(int j = , P = ; j < i; j++){
r1 = r1 + P * temp[j];
P = P * ;
}
sort(temp, temp + i, cmp2);
for(int j = , P = ; j < i; j++){
r2 = r2 + P * temp[j];
P = P * ;
}
}
int main(){
int N, r1, r2, ans;
scanf("%d", &N);
numSort(N, r1, r2);
do{
ans = r1 - r2;
printf("%04d - %04d = %04d\n", r1, r2, ans);
numSort(ans, r1, r2);
}while(ans != && ans != );
cin >> N;
return ;
}

总结:

1、注意在int转换为num[ ]数组时,如果不够四位,应补全成四位,否则答案会出错。(15应转换为0015和1500,而不是15和50)。

A1069. The Black Hole of Numbers的更多相关文章

  1. APT甲级——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 ...

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

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

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

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

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

  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(20 分)

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...

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

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

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

随机推荐

  1. 记一次用WPScan辅助渗透WordPress站点

    记一次用WPScan辅助渗透WordPress站点 一.什么是WPScan? WPScan 是一个扫描 WordPress 漏洞的黑盒子扫描器,它可以为所有 Web 开发人员扫描 WordPress ...

  2. windows 脚本

    sudo.vbs http://blog.csdn.net/qidi_huang/article/details/52242053 c:\windows\sudo.vbs 'ShellExecute ...

  3. Shell学习笔记一

    Shell 简介 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. 基本上Shell分两大类:一:图形界面Shell(G ...

  4. 网络流第一题!!!BZOJ1001

    歇逼了一晚上,懵懵懂懂的懂了Dinic算法 大概是一遍BFS+DFS,还不是很懂,明天继续看!!! #include<iostream> #include<stdio.h> # ...

  5. Scrum Meeting 6

                第六次会议 由于之前队员一直在做数据库和编译大作业,课业压力较大,所以软工进度往后拖了好多. No_00:工作情况 No_01:任务说明 待完成 已完成 No_10:燃尽图 N ...

  6. Redis持久化的两种方式和区别

    该文转载自:http://www.cnblogs.com/swyi/p/6093763.html Redis持久化的两种方式和区别 Redis是一种高级key-value数据库.它跟memcached ...

  7. ABP框架用Dapper实现通过SQL访问数据库

    ABP的框架(2) - 访问数据库   为了防止不提供原网址的转载,特在这里加上原文链接:http://www.cnblogs.com/skabyy/p/7517397.html 本篇我们实现数据库的 ...

  8. Spring Boot features - Profiles

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html https://w ...

  9. Building Java Projects with Gradle

    https://spring.io/guides/gs/gradle/ Gradle学习系列教程 https://blog.csdn.net/column/details/gradle-transla ...

  10. 为什么要用Thrift

    Why Thrift, Why not HTTP RPC(JSON+gzip) https://stackoverflow.com/questions/9732381/why-thrift-why-n ...