The Black Hole of Numbers (strtoint+inttostr+sort)
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
坑点1、数字都要四位的2、如果是判断下一个式子的差不等于上个结果,然后跳出的话,那么需要判断,是不是第一次输出。比如,输入6174,结果就等于6174,那么就会没输出,直接跳出。
#include <iostream> #include <algorithm> #include<string> #include <sstream> #include <iomanip> using namespace std; int a1[]; int a2[]; int bb[]; bool cmp1(int a,int b) { return a>b; } bool cmp2(int a,int b) { return a<b; } int main() { string n; int i; while(cin>>n) { int tt,c1,c2; stringstream ss1; ss1<<n; ss1>>tt; i=; bool fir=true; while(true) { string ss; stringstream ss2; ss2<<setfill('')<<setw()<<tt; ss2>>ss; for(i=;i<ss.length();i++) { a1[i]=ss[i]-''; a2[i]=a1[i]; } sort(a1,a1+ss.length(),cmp1); sort(a2,a2+ss.length(),cmp2); c1=; c2=; for(i=;i<ss.length();i++) { c1=c1*+a1[i]; c2=c2*+a2[i]; } if(c1-c2==tt&&!fir) break; else { fir=false; cout<<setfill('')<<setw()<<c1<<" - "<<setfill('')<<setw()<<c2<<" = "<<setfill('')<<setw()<<c1-c2<<endl; tt=c1-c2; } } } return ; }
The Black Hole of Numbers (strtoint+inttostr+sort)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 临时2级页表的初始化过程 head_32.S 相关代码解释
page_pde_offset = (__PAGE_OFFSET >> 20); /* __PAGE_OFFSET是0xc0000000,page_pde_offset = 3072 = ...
- 转载:Restore SQL Server database and overwrite existing database
转载自:https://www.mssqltips.com/sqlservertutorial/121/restore-sql-server-database-and-overwrite-existi ...
- html的标签中 unselectable=on 属性的作用
在IE浏览器中,当input获得焦点时,点击有unselectable="on"属性的标签时,不会触发onblur事件. 加上该属性的元素不能被选中. < !DOCTYPE ...
- HDU 2181 哈密顿绕行世界问题 (DFS)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- centos 安装 acrobat Reader之后
IV: 为Firefox等浏览器安装Acrobat Reader插件:sudo /usr/local/Adobe/Acrobat7.0/Browser/install_browser_plugin按照 ...
- C# WPF 连接数据库Sqlhelper类
从视频上学习到的WPF连接数据库的方法 需要配置一个 configuration 文件,包含 SQL instance 的相关信息 using System; using System.Collect ...
- asp.net微信支付打通发货通知代码
上次遇到微信支付,发货接口的时候,官方的demo也没有提供相应的代码 ,因本人技术有限,百度 google 很久都没有asp.net 版本的,最后只好硬着头皮自己搞,没想到官方文档也是错的. 我这一步 ...
- 【AngularJs】---Error: [$injector:modulerr] Failed to instantiate module starter.services
[遇到问题解决问题,原谅我这个菜鸟] 加了services angular.module('starter', ['ionic', 'starter.controllers', 'starter.se ...
- Spring与Hibernate整合之通用Dao的实现
在上一篇文章中写了如何直接利用HibernateTemplate进行数据库操作,但在一般的项目中很少直接得到HibernateTemplate的Bean对象从而操作数据库的,下面就简要介绍一下实现通用 ...
- 如何检查mysql中建立的索引是否生效的检测方法及相关参数说明
所使用的mysql函数explain语法:explain < table_name >例如: explain select * from t3 where id=3952602;expla ...