poj解题报告——1350
2015-07-17 13:41
357 查看
输入一个数,对这个数的每位排序,分别排成一个最大数和最小数,求其差作为下次操作的数,直到差为0或者6174位置
#include<cstdio> #include<algorithm> using namespace std; int cant(int s) { int a=s/1000; int b=s/100%10; int c=s/10%10; int d=s%10; if(a==b&&b==c&&c==d) return 1; return 0; } int cal(int s) { int bn=s; int a[4]; a[0]=s/1000; a[1]=s/100%10; a[2]=s/10%10; a[3]=s%10; int fl=0; if(a[0]==0) { fl=1; if(a[1]==1) { fl=2; if(a[2]=0) fl=3; } } sort(a,a+4); int bnum,snum; snum=a[0]*1000+a[1]*100+a[2]*10+a[3]; if(fl==0) { bnum=a[3]*1000+a[2]*100+a[1]*10+a[0]; } else if(fl==1) { bnum=a[3]*100+a[2]*10+a[1]; } else if(fl==2) { bnum=a[3]*10+a[2]; } else { bnum=a[3]; } printf("%d-%d=%d\n",bnum,snum,bnum-snum); return bnum-snum; } int main() { int n; while(scanf("%d",&n)&&n!=-1) { printf("N=%d:\n",n); if(n>=9999||n<=1000||cant(n)) { printf("No!!\n"); continue; } int times=0; while(n!=0&&n!=6174) { times++; n=cal(n); } printf("Ok!! %d times\n",times); } return 0; }
相关文章推荐
- 骑士cms最新全局SQL注入(官方奇葩修复案例)
- Centos 6 克隆导致网卡eth0变成eth1、及修改网卡名的方法
- 黑马程序员——正则表达式、反射
- BZOJ3711 : [PA2014]Druzyny
- 文件同步(rsync + inotify ) , sersync 镜像文件
- Unity json
- 网上商城之购物车的3种实现方式
- 动态绑定
- hdu 1007 Quoit Design
- 【转载】OmniGraffle (五)对象的控制
- LeetCode#204 Count Prime
- 常用正则表达式
- ViewDragHelper让你处理View拖动时,代码减半!
- vbs经典回顾之[键盘输入方法]wshShell.SendKeys
- jquery checkbox trigger click 顺序问题
- 虚拟机Ubuntu终端中划词选择时命令终止(^C)的解决办法
- c++课程实训 银行储蓄系统
- 挨踢(IT)脱口秀--2014年&2015上半年特别“任性”的网络流行语大盘点
- 挨踢(IT)脱口秀--2014年&2015上半年特别“任性”的网络流行语大盘点
- 挨踢(IT)脱口秀--2014年&2015上半年特别“任性”的网络流行语大盘点