pat1069. 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 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<stack>
- #include<cstring>
- #include<iostream>
- #include<stack>
- #include<set>
- #include<map>
- using namespace std;
- int dight[];
- int main()
- {
- //freopen("D:\\INPUT.txt","r",stdin);
- int n,maxnum,minnum;
- scanf("%d",&n);
- int i,j;
- do{//有可能n一开始就是6174
- for(i=; i>=; i--)
- {
- dight[i]=n%;
- n/=;
- }
- for(i=; i<; i++)//由大到小
- {
- maxnum=i;
- for(j=i+; j<; j++)
- {
- if(dight[j]>dight[maxnum])
- {
- maxnum=j;
- }
- }
- int t=dight[maxnum];
- dight[maxnum]=dight[i];
- dight[i]=t;
- }
- minnum=;
- for(i=; i>=; i--)
- {
- minnum*=;
- minnum+=dight[i];
- }
- maxnum=;
- for(i=; i<; i++)
- {
- maxnum*=;
- maxnum+=dight[i];
- }
- n=maxnum-minnum;
- printf("%04d - %04d = %04d\n",maxnum,minnum,n);
- if(!n)
- break;
- }while(n!=);
- return ;
- }
pat1069. The Black Hole of Numbers (20)的更多相关文章
- 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 分)(内含别人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), ...
- 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 ...
- 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 ...
- 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 ...
- PAT1069. The Black Hole of Numbers
//这是到水题,之前因为四位数的原因一直不能A,看了别人的程序,才明白,不够四位的时候没考虑到,坑啊.....脸打肿 #include<cstdio>#include<algorit ...
- PAT (Advanced Level) 1069. The Black Hole of Numbers (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1069. The Black Hole of Numbers (20)-模拟
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789244.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- mina中的发送延时
由于项目需要,用到了 mina 框架进行 tcp 通讯.我是初次接触 mina,于是从 Hello world 开始学习了 mina .期间遇到了一个奇怪的发送数据的延迟问题,解决的过程是曲折的,但找 ...
- 安装mariadb并修改配置文件
实验环境:CentOS7 #安装mariadb-server包#修改mariadb配置文件/etc/my.cnf.d/server.cnf#添加 skip_name_resolve=ON #不执行将I ...
- 4.JasperReports学习笔记4-查询数据库生成动态的报表(WEB)
转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html 第一种方式: sql语句中定义查询条件,报表中定义接收参数 第二种方式: ...
- 调试opencv调用摄像头程序时碰到的问题
昨天晚上想把opencv学习笔记整理一下,当跑opencv调用摄像头的程序的时候老是出现Assertion failed (size.width>0 && size.height ...
- 差一点搞混了Transactional注解
今天给我的Srping业务层加如下Service和Transactional注解: @Service @Scope(BeanDefinition.SCOPE_SINGLETON) @Transacti ...
- Java探索之旅(1)——概述与控制台输入
使用的课本: Java语言程序设计(基础篇)----西电 李娜(译) 原著: Introduction to Java Progrmming(Eighth Edition) -----Y.Daniel ...
- shell批量创建文件及改名
批量创建文件及改名企业面试题2:使用for循环在/usr/sunzy目录下通过随机小写10个字母,批量创建10个html文件. #!/bin/bash Path=/usr/sunzy [ -d $Pa ...
- Java之反射(部分文档摘过来方便以后查看)
第1章 类加载器 1.1 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. l 加载 就是指将class文件读入内存,并为之创建 ...
- 6.JBoss5.x6.x 反序列化漏洞(CVE-2017-12149)复现
2017 年 9 月 14 日,国家信息安全漏洞共享平台( CNVD )收录了 JBOSS Application Server 反序列化命令执行漏洞( CNVD-2017-33724,对应 CVE- ...
- hdu1069
#include <iostream> #include <algorithm> #include <cstring> using namespace std; c ...