题目内容:
假设有这样一个三位数m,其百位、十位和个位数字分别是a、b、c,如果m= a!+b!+c!,则这个三位数就称为三位阶乘和数(约定0!=1)。请编程计算并输出所有的三位阶乘和数。
函数原型: long Fact(int n);
函数功能:计算n的阶乘
输入格式: 无
输出格式:"%d\n"
我的代码:
#include
main()
{
int m;
int a,b,c;
m = 100*a + 10*b + c;
if ((m == long Fact(a) + long Fact(b) + long Fact(c))=1)
{
printf("%d\n", m);
}
else
{
break;
}
long Fact(int n);
{
int i, n;
int result;
for (i =1; i<=n; i++);
{
result *=i;
}
return result;
}
error:
请问该怎么改这段代码?
不逐一修改了,直接给出代码:
#include<stdio.h>
long Fact(int n); /*自定义函数说明*/
void main()
{
int hundred, ten, one, m, n;
printf("三位阶乘和数有:");
for (hundred = 1;hundred <= 6;hundred++)
for (ten = 0;ten <= 6;ten++)
for (one = 0;one <= 6;one++)
{
m = hundred * 100 + ten * 10 + one;
n = Fact(hundred) + Fact(ten) + Fact(one);
if (m == n) /*阶乘和条件判别*/
printf("%d\n", n);
}
}
long Fact(int n)
{
int i;
long s = 1;
for (i = 1;i <= n;i++)
s *= i;
return(s);
}
说明:因为7!=5040>999,所以a,b,c必然小于7。当然也可以把循环条件设置为<=9,让电脑去处理,不影响结果。
运算结果:
**三位阶乘和数有:145 **
//计算三位阶乘和数 2018.11.08
#include
int Fact(int n);
int main()
{
int a, b, c, i;
long int m, n;
for(n=100; n<=999; n++ )
{
a = n/100;
b = n/10%10;
c = n%10;
m = Fact(a) + Fact(b) + Fact(c);
if(n == m)
printf("%ld\n", m);
}
return 0;
}
int Fact(int n)
{
int s=1;
int i = 1;
for(i = 1; i<=n; i++)
{
s*=i;
}
return s;
}