不用循环控制、条件控制、三目运算符 实现,不考虑溢出
long func(int n)
{
( n <= 1 && (n=1) ) || ( n*=func(n-1));
return n;
}
template<int N>
struct Factorial
{
enum { Value = N * Factorial<N - 1>::Value };
};
template<>
struct Factorial<1>
{
enum { Value = 1 };
};
计算100!
void getn(int n ,char *buf)
{
int arr[256];
int index;
arr[0] = 1;
index = 1;
for(int i = 1; i <= n; ++i)
{
int movebit = 0; // 进位值
for(int j = 0; j < index; ++j)
{
arr[j] = arr[j]*i + movebit;
movebit = arr[j]/10;
arr[j] %= 10;
}
while(movebit)
{
arr[index++] = movebit % 10;
movebit /= 10;
}
}
//将数组倒序赋值给字符串
for(int i = index - 1; i >= 0; i--)
{
*buf++ = arr[i] + '0';
}
*buf = '\0';
return;
}