小编Lim*_*mon的帖子

为什么gets(stdin)返回整数?等错误

我是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)

输入参数的值: …

c compiler-errors gets standard-library

3
推荐指数
1
解决办法
720
查看次数

标签 统计

c ×1

compiler-errors ×1

gets ×1

standard-library ×1