阶乘C语言没有条件语句,循环和算术运算符阶乘、算术、语句、运算符

2023-09-11 03:41:40 作者:把心還給我

我怎么能找到的一个数字(从1到10)C,阶乘不使用:

How can I find the factorial of a number (from 1 to 10) in C, without using:

在循环语句像,同时,做一下; 在有条件的经营者一样,如果和情况;和 算术运算符如+, - ,*,%,/,+, - ?

FYI:我发现C语言天赋这个问题

FYI: I found this question in C aptitude.

推荐答案

由于只有1到10,只需precompute它并将其存储在大小11的一个简单的int数组的数组中的第一个元素把1.它是不是有效的输入范围的问题,但很可能会成为正确的。

Since it is only 1 to 10, simply precompute it and store it in a simple int array of size 11. For the first element in the array put 1. It is not a valid input range for your problem but might as well be correct.

我们需要存储11元​​素,而不是我们需要的,否则我们就需要使用运行的10 - ,以获得正确的索引。减法是不是你的问题,但允许

We need to store 11 elements instead of the 10 we need because otherwise we'd need to use operation "-" to get the right index. Subtraction is not allowed in your problem though.

int factorial(int x)
{
  return precomputedArray[x];
}