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 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的更多相关文章
- 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 ...
- 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 ...
- 1069 The Black Hole of Numbers (20分)
1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...
随机推荐
- floyd类型题UVa-10099-The Tourist Guide +Frogger POJ - 2253
The Tourist Guide Mr. G. works as a tourist guide. His current assignment is to take some tourists f ...
- elementUI中的隐藏组件el-scrollbar
细心的人儿都会发现elementUI官网的滚动条样式优美,但是elementUI中并未给出这个滚动条组件,打开调试页面发现用到了el-scrollbar.问一下度娘发现早就有前辈们发现了这个问题并给出 ...
- POJ - 2774~POJ - 3415 后缀数组求解公共字串问题
POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...
- Linux-c 线程锁
typedef struct _my_mutex { pthread_mutex_t mutex; //互斥锁 pthread_mutexattr_t mta; //互斥锁属性 } my_mute ...
- css3 ---1 基本的选择器
基本的选择器 <style type="text/css"> /*通配符选择器*/ * { margin: ; padding: ; border: none; } / ...
- 中国剩余定理模数互质的情况模板(poj1006
http://poj.org/problem?id=1006 #include <iostream> #include <cstdio> #include <queue& ...
- 对比两个String无规律包含连续4个相同字符返回true的方法
package com.qif.dsa.util; import java.util.ArrayList; import java.util.List; /** * @author * @Title: ...
- mysql 存中文失败问题
在向数据库存中文时报错: 将中文字段的字符集改为utf8即可: 成功截图:
- C#可扩展编程之MEF(五):MEF高级进阶
好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较 ...
- 使用XPath查询带有命名空间(有xmlns)的XML(转)
使用XPath查询带有命名空间(有xmlns)的XML 标签: xmlsilverlightwebserviceencodingwpfinclude 2012-06-19 10:26 3235人阅读 ...