解题思路:
将阶乘和数存入数组,利用数组排序(只是为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 人评分
C语言程序设计教程(第三版)课后习题8.9 (Java代码)浏览:1217 |
不容易系列2 (C语言代码)浏览:564 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:720 |
小明A+B (C语言代码)浏览:1141 |
大神老白 (C语言代码)浏览:596 |
校门外的树 (C语言代码)浏览:936 |
C语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:1199 |
wu-淘淘的名单 (C++代码)浏览:1279 |
WU-小九九 (C++代码)浏览:1649 |
DNA (C语言代码)浏览:367 |