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 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的更多相关文章
- 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 ...
- 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 ...
- 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), ...
随机推荐
- nginx反向代理中proxy_set_header 运维笔记
Nginx proxy_set_header:即允许重新定义或添加字段传递给代理服务器的请求头.该值可以包含文本.变量和它们的组合.在没有定义proxy_set_header时会继承之前定义的值.默认 ...
- Python_复习_习题_29
# 之前做得的题 以后再遇到能保证会# 下周二考 :所有的知识# 面试题:认真对待## 三元运算符# 接收结果的变量 = 条件为真的结果 if 条件 else 条件为假的结果# 接收结果的变量 = “ ...
- ULMFiT 阅读笔记
ULMFiT 阅读笔记 概述 这篇文章从文本分类模型入手,主要提出了两点:一是预训练语言模型在大中小规模的数据集中都能提升分类效果,在小规模数据集中效果尤为显著.二是提出了多种预训练的调参方法,包括D ...
- Linux内核设计第十七章笔记
第十七章 设备与模块 关于设备驱动和设备管理,四种内核成分 设备类型:在所有unix系统中为了统一普通设备的操作所采用的分类 模块:Linux内核中用于按需加载和卸载目标代码的机制 内核对象:内核数据 ...
- Jquery画折线图、柱状图、饼图
1.今天做了一个折线图,首先需要导js文件.这里有一个demo:http://files.cnblogs.com/files/feifeishi/jquery_zhexiantubingtuzhuzh ...
- MySQL存储和获取数据
---恢复内容开始--- 一.MySQL存储引擎? 1.引擎定义 存储引擎其实就是如何存取数据,如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法.因为在关系型数据苦中数据是以表格的形式,所 ...
- XMLHttpRequest详解
XMLHttpRequest详解: https://xhr.spec.whatwg.org/
- vue 路由传参 params 与 query两种方式的区别
初学vue的时候,不知道如何在方法中跳转界面并传参,百度过后,了解到两种方式,params 与 query.然后,错误就这么来了: router文件下index.js里面,是这么定义路由的: { p ...
- [转帖]UML各种图总结-精华
UML各种图总结-精华 https://www.cnblogs.com/jiangds/p/6596595.html 之前自己以为画图很简单 不需要用心学 现在发现自己一直没有学会一些基础的知识 能力 ...
- Jquery 组 表单验证
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...