解题思路:

将阶乘和数存入数组,利用数组排序(只是为a[i]和a[i+1]的首位大小比较)

注意事项:





参考代码:

#include <stdio.h>

#include <math.h>

int fun(int n);//阶乘递归函数

int main()

{

int n,s,a[32],i=0,count;

for(n=1;n<=100000;n++){//就当全部符合,所有n<=10万

int temp=n;

s=0;

while(temp){

s+=fun(temp%10);

temp/=10;

}

if(n==s)

a[i++]=n;//将阶乘和数存入数组好按要求排序

}

count=i;//保存数组大小

int j;

/*利用数组排序(只是为a[i]和a[i+1]的首位大小比较)*/

for(j=0;j<count-1;j++){

int flag=0;

for(i=0;i<count-1-j;i++){

int temp=a[i],tep=a[i+1],s1;

while(temp){

s=temp%10;

temp/=10;

}

while(tep){

s1=tep%10;

tep/=10;

}

if(s>s1){

flag=1;

temp=a[i];

a[i]=a[i+1];

a[i+1]=temp;

}

}

if(!flag)

break;

}

for(i=0;i<count;i++)

printf("%d ",a[i]);

return 0;

}


int fun(int n)

{

if(n>1)

return n*fun(n-1);

else

return 1;

}


 

0.0分

3 人评分

  评论区