6174问题

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
 
描述

假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b,然后用a-b替换原来这个数,并且继续操作。例如,从1234出发,依次可以得到4321-1234=3087、8730-378=8352、8532-2358=6174,又回到了它自己!现在要你写一个程序来判断一个四位数经过多少次这样的操作能出现循环,并且求出操作的次数

比如输入1234执行顺序是1234->3087->8352->6174->6174,输出是4

 
输入
第一行输入n,代表有n组测试数据。
接下来n行每行都写一个各位数字互不相同的四位数
输出
经过多少次上面描述的操作才能出现循环
样例输入
1
1234
样例输出
4
 #include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char a[],b[];int ia,ib; int main()
{
int N,count,temp=;
cin>>N;
while(N--)
{
count=;
for(int i=;i<;i++)
cin>>a[i];
loop:
ia=ib=;
sort(a,a+);
for(int i=;i<;i++)
{
ia*=;
ia+=(a[i]-'');
}
for(int i=;i>=;i--)
{
ib*=;
ib+=(a[i]-'');
}
ib=ib-ia;
count++;
if(temp!=ib)
{
temp=ib;
for(int i=;i<;i++)
{
a[i]=(ib%+'');
ib/=;
}
goto loop;
}
else
{
cout<<count<<endl;
}
} }
 #include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
int k;
cin>>k;
while(k--)
{
int n,a[],n1,n2;
scanf("%d",&n);
int s=;
while(n!=)
{
a[]=n%;
a[]=n/;
a[]=n/%;
a[]=n/%;
sort(a,a+);
n1=*a[]+*a[]+*a[]+a[];
n2=*a[]+*a[]+*a[]+a[];
n=n1-n2;
s++;
}
printf("%d\n",s);
}
}

最新文章

  1. Eclipse代码和xml文件的智能提示
  2. OSG中的HUD
  3. [算法导论]红黑树实现(插入和删除) @ Python
  4. 基于h5的图片无刷新上传(uploadifive)
  5. P1119 灾后重建
  6. [转载]MongoDB 常用命令
  7. PHP计算中文字符串长度 、截取相应中文字符串
  8. Python中的日期和时间
  9. 【shell mysql 导出数据到csv脚本,完美解决乱码转义符等问题】-费元星
  10. UNIX网络编程——客户/服务器程序设计示范(六)
  11. Java-正则使用
  12. linux文件系统启动流程、启动脚本
  13. javascript与jquery的区别
  14. __getitem__ __setitem__ __delitem__ 使用
  15. pip更新到18版本后使用pycharm更新问题:module &#39;pip&#39; has no attribute &#39;main&#39;
  16. springmvc 动态加载配置文件
  17. python记录_day22 序列化
  18. java 中 enum 枚举的使用
  19. zookeeper应用 - leader选举 锁
  20. 奇怪的等待事件“enq: ss - contention”

热门文章

  1. php对数组中的键与值进行合并处理
  2. 框架基础:ajax设计方案(五)--- 集成promise规范,更优雅的书写代码
  3. [leetcode-485-Max Consecutive Ones]
  4. 使用Homebrew安装MySQL
  5. 游戏UI框架设计(7): 资源国际化技术
  6. 同网段电脑互ping
  7. 怎样把echarts图表做成响应式的
  8. Linux下进行硬盘挂载、分区、删除分区,格式化,卸载方法
  9. python基础(4):条件语句与循环语句
  10. github 项目管理