【发布时间】:2018-08-04 10:09:03
【问题描述】:
我们必须打印一个系列的第 n 项,其前三项为a, b, c,第 n 项是前三项的总和。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.
int find_nth_term(int n, int a, int b, int c) {
//Write your code here.
int i, arr[n];
arr[0] = a;
arr[1] = b;
arr[2] = c;
if (n >= 3 && i <= n) {
arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3];
//using recursion to find nth term
return find_nth_term(n, a, b, c);
} else {
return;
}
}
int main() {
int n, a, b, c;
scanf("%d %d %d %d", &n, &a, &b, &c);
int ans = find_nth_term(n, a, b, c);
printf("%d", ans);
return 0;
}
【问题讨论】:
-
find_nth_term()函数的返回类型是int,但您似乎没有返回实际值。您的return语句在else语句中为空。 -
另外,你的
i永远不会被初始化。 -
@Inrin 但是,当我运行代码时,这不会引发任何错误。
-
用
-Wall编译你的代码,你的编译器会报错。你没有遇到段错误是纯粹的(坏)运气。
标签: c