写一个递归函数
用函数的递归调用方法求下列函数式的值(要求n由用户输入),递归公式为:
total(n)=1 (n=1);
total(n)=total(n-1)+n+1 (n>1).
#include <stdio.h>
int total(int n){
if(n==1){
return 1;
}else if(n>1){
return total(n-1)+n+1;
}
return 0; //可省略
}
int main(int argc, char *argv[]){
int n;
scanf("%d",&n);
int m = total(n);
printf("%d",m);
return 0;
}