编写递归函数int fint n实现如下功能:有n个数后一个数比前一个数大n-2已知第1个数是2那么求第n个数的大小。在main函数中输入n调用f函数后在main函数中输出结果。【输入形式】【输出形式】【样例输入】8【样例输出】23#include stdioh#include stdlibhint fint n;int main int n; scanfd&n;
#include <stdio.h> int f(int n) { if(n == 1) return 2; // 递归边界 return f(n - 1) + n - 2; // 递归式 } int main() { int n; scanf("%d", &n); printf("%d", f(n)); return 0; }
原文地址: https://www.cveoy.top/t/topic/eOp5 著作权归作者所有。请勿转载和采集!