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 ...
随机推荐
- Android 高级UI设计笔记05:使用TextView实现跑马灯的效果
1. 使用TextView属性实现跑马灯的效果: (1). 新建一个Android工程,命名为"MarqueeTextViewDemo",如下: (2). 来到activity_m ...
- ArcMap运行时出现Runtime Error错误的解决方案
运行ArcMap时弹出错误提示:“Microsoft Visual C++ Runtime Library. Runtime 1.开始->运行->regsvr32 "C:\Pro ...
- Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary
NSDictionary(字典),NSDictionary类似于 .net中的parameter,l类似于java中的map. 通过唯一的key找到对应的值,一个key只能对应一个只,而多个key可以 ...
- 常用html演示模板
--学习测试新的css,js,经常用到test.html,在这里放一个: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...
- Bash循环分类介绍
方法一: #!/bin/bash ` do #code here echo $i done 方法二:C语言风格 #!/bin/bash ; i<=; i++)) do printf " ...
- 在IIS7.5上添加.NET4.0程序的虚拟目录时提示ASP.NET 4.0尚未在 Web 服务器上注册
使用VS2010创建web应用程序时出现如下提示: ASP.NET 4.0尚未在 Web 服务器上注册. 解决方法: 首先IIS应用程序池添加 net framework4.0版本. 接着找到C:\W ...
- C++STL学习笔记_(4)queue
10.2.5Queue容器 Queue简介 ² queue是队列容器,是一种"先进先出"的容器. ² queue是简单地装饰deque容器而成为另外的一种容器. ² #inc ...
- Linux 命令 - su: 以其他用户和组 ID 的身份来运行 shell
在 shell 会话状态下,使用 su 命令将允许你假定为另一个用户的身份,既可以以这个用户的 ID 来启动一个新的 shell 会话,也可以以这个用户的身份来发布一个命令. 命令格式 su [OPT ...
- C#算法基础之选择排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- select into 、 insert into select 、create table as select复制表
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但 ...