解题思路:
将阶乘和数存入数组,利用数组排序(只是为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 人评分
简单的a+b (C语言代码)浏览:809 |
Pascal三角 (C语言代码)格式错误浏览:550 |
大神老白 (C语言代码)浏览:689 |
本人酷爱递归实现很多问题,这里也是浏览:606 |
三角形 (C++代码)递归(存在大量重复计算,容易出现时间超限)浏览:832 |
2005年春浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:634 |
用筛法求之N内的素数。 (C语言代码)浏览:709 |
Cylinder (C语言描述+详细分析)浏览:3351 |
1017题解浏览:663 |
母牛的故事 (C语言代码)浏览:1022 |