1019. 数字黑洞 (20)
2015-07-08 18:11
288 查看
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的6174,这个神奇的数字也叫Kaprekar常数。
例如,我们从6767开始,将得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...
现给定任意4位正整数,请编写程序演示到达黑洞的过程。
输入格式:
输入给出一个(0, 10000)区间内的正整数N。
输出格式:
如果N的4位数字全相等,则在一行内输出“N - N = 0000”;否则将计算的每一步在一行内输出,直到6174作为差出现,输出格式见样例。注意每个数字按4位数格式输出。
输入样例1:
6767
输出样例1:
7766 - 6677 = 1089 9810 - 0189 = 9621 9621 - 1269 = 8352 8532 - 2358 = 6174
输入样例2:
2222
输出样例2:
2222 - 2222 = 0000
#include <iostream> #include <cmath> #include <stdio.h> using namespace std; void Shengxu(int a[],int n); int main(int argc, char *argv[]) { int n; while(cin>>n) { int m; m=n; if(6174==m) { cout<<"7641 - 1467 = 6174"<<endl; } while(m!=6174) { int a[4]={0}; int i=0; while(m>0) { a[i]=m%10; m=m/10; i++; } int num1=0,num2=0; if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]) { cout<<n<<" - "<<n<<" = "<<"0000"<<endl; break; } else { Shengxu(a,4); for(i=3;i>=0;i--) { cout<<a[i]; num1=num1+a[i]*(int)pow(10,i); } cout<<" - "; for(i=0;i<4;i++) { cout<<a[i]; num2=num2+a[i]*(int)pow(10,3-i); } cout<<" = "; m=num1-num2; printf("%04d\n",m); } } } return 0; } void Shengxu(int a[],int n) { int i,j; int temp; int sum=0; for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; } } } }
相关文章推荐
- Java面向对象的思想
- c++基本特性
- [leedcode 25] Reverse Nodes in k-Group
- Android中的进程和pid
- Web前端知识技能大汇总
- AFHTTPClient的异步回调模式
- GDB十分钟教程
- 【第一篇】学习 android 事件总线androidEventbus之sticky事件的传递
- Linux 基础命令知识3
- LeetCode-Course Schedule-解题报告
- linux 下的sar工具命令小结
- Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- 如何快速高效的更新memcached缓存数据
- Window Server 2012 R2 下 IE11 浏览器 无法安装Flash 怎么解决
- php 常用的好函数(持续更新)
- 【GLSL教程】(一)图形流水线
- python子类调用父类的方法-super
- web terminal 探究:shellinabox使用心得
- could not build module foundation
- While reading XXX pngcrush caught libpng error: