用C++编写:求这样一个三位数,该三位数等于其每位数字的阶乘之和

2024-12-03 19:45:50
推荐回答(2个)
回答1:

int main()
{
int factor[7] = {1, 1, 2, 6, 24, 120, 720}; //7的阶乘已大于3位
int n = 100;
int result = 0;
for(; n < 1000; n++)
{
result = factor[n % 10] + factor[n / 10] + factor[n / 100];
if (n == result)
{
printf("\r\nnum:%d", n);
}
}

return 0;
}

大概这样,需要自己调试

回答2:

if(i=jiech(a)+jiech(b)+jiech(c))
改成
if(i==jiech(a)+jiech(b)+jiech(c))

结果如下

#include
main ()
{
int jiech (int k);
int a, b, c, i;
for (i = 100; i <= 999; i++)
{
a = i / 100;
b = i % 100 / 10;
c = i % 10;
if (i == jiech (a) + jiech (b) + jiech (c))
printf ("%4d\n", i);
}
}

jiech (int k)
{
int n = 1, m = 1;
while (n <= k)
{
m = m * n;
n++;
}
return (m);
}

答案 145
另外,站长团上有产品团购,便宜有保证