6174问题

xiaoxiao2025-11-02  4

#include <iostream> #include<algorithm> using namespace std;  /* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) { int t,n,a[4],n1,n2,s; cin>>t; while(t--){     cin>>n;     s=1;     while(n!=6174){         a[0]=n%10;         a[1]=n/1000;         a[2]=n/10%10;         a[3]=n/100%10;         sort(a,a+4);         n1=1000*a[3]+100*a[2]+10*a[1]+a[0];         n2=1000*a[0]+100*a[1]+10*a[2]+a[3];         n=n1-n2;         s++;     }     cout<<s<<endl; }     return 0; }

 

当我们使用c语言时有很多已经包装好的算法不能使用,所以这时我们需要用c++来进行解题。

其中sort(a,a+4);

是sort算法里的快速排序

算法如下:

sort(a,a+4){

int i,j,t;

for(i=a;i<a+4;i++)

for(j=a;j<a+4;j++)

if(a[i]>a[j]){

t=a[i];

a[i]=a[j];

a[j]=t;}

}

 

转载请注明原文地址: https://www.6miu.com/read-5038922.html

最新回复(0)