题目给了举例,应该不难看懂。
#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.
if (n == 1){ //n==1,返回a,这是题目要求
return a;
}
else if (n == 2){ //如果不是,但又满足2,返回b,同上,是题目要求
return b;
}
else if (n == 3){ //同上
return c;
}
else { //如果都不满足,就开始递归了。比如n为5,返回f(4)+f(3)+f(2),后面俩简单,f(4)开始新递归,n为4,又成f(3)+f(2)+f(1).以此类推
return find_nth_term(n-1,a,b,c) + find_nth_term(n-2,a,b,c) + find_nth_term(n-3,a,b,c);
}
}
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;
}