6174问题
假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b,然后用a-b替换原来这个数,并且继续操作。例如,从1234出发,依次可以得到4321-1234=3087、8730-378=8352、8532-2358=6174,又回到了它自己!现在要你写一个程序来判断一个四位数经过多少次这样的操作能出现循环,并且求出操作的次数
比如输入1234执行顺序是1234->3087->8352->6174->6174,输出是4
1 1234
4
#include <stdio.h>
#include <algorithm>
#include <iostream>
int main()
{
int x,t,count;
int n[4];
scanf("%d",&t);
while(t--)
{
count=1;
scanf("%d",&x);
while(x!=6174)
{
n[0]=x%10;
n[1]=x%100/10;
n[2]=x/100%10;
n[3]=x/1000;
std::sort(n,n+4);
x=(n[3]*1000+n[2]*100+n[1]*10+n[0])
-(n[0]*1000+n[1]*100+n[2]*10+n[3]);
count++;
}
printf("%d\n",count);
}
return 0;
}
原文:https://www.cnblogs.com/darkchii/p/9164899.html