HzuMRL


私信TA

用户名:1710819057

访问量:11581

签 名:

算法菜鸡萌新

等  级
排  名 441
经  验 4711
参赛次数 7
文章发表 27
年  龄 0
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

解题思路:
    

                拆分a,存到数组,从小到大排序和为X,从大到小排序和为Y,求Y-X 最后while()循环 a=Y-X;
注意事项:

参考代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e7;
int ans[maxn];
int sum(int ans[])
{
      int sum=0;
     for(int s=0;s<4;s++)
     {
      sum=sum*10+ans[s];
     }
     return sum;
}
bool cmp(int a,int b)
{
     return a>b;
}     
int main()
{
     int a,i,num=0,score=0;
     cin>>a;
     while(score!=6174)
      {
       i=0;
      while(a)
      {
      ans[i]=a%10;
       a/=10;
       i++;
      }
  sort(ans,ans+4);
  int x=sum(ans);
  sort(ans,ans+4,cmp);//从大到小排序
  int y=sum(ans);
  score=y-x;
  //cout<<score<<endl;
  a=score;
  num++;
  }
 cout<<num<<endl;
 return 0;
}

 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区