我是C编程新手(尽管我有Java经验)。阅读了一些教程之后,我决定开始解决Coderbyte上的编码难题。
我尝试的第一个挑战是这一个:
挑战
让函数FirstFactorial(num)接受传递的num参数并返回其阶乘。例如:如果num = 4,则您的程序应返回(4 * 3 * 2 * 1) =24。对于测试用例,范围将在1到18之间,并且输入将始终是整数。
样本测试用例
输入:4
输出:24输入:8
输出:40320
我的解决方案:
#include <stdio.h>
void FirstFactorial(int num[]) {
int i = num -1;
for(i ; i > 0; i--) {
num = num * i;
printf("%d",i);
}
printf("\t %d", num);
}
int main(void) {
// disable stdout buffering
setvbuf(stdout, NULL, _IONBF, 0);
// keep this function call here
FirstFactorial(gets(stdin));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输入参数的值: …