jeck猫
一、递归的写法:123456789101112131415#include <stdio.h>int g(int m,int n);int main(){ printf("g(3,5)的结果:%d\n",g(3,5)); return 0;}int g(int m,int n){ if(m==0 && n>=0) return 0; else if(m>0 && n>=0) return g(m-1,2*n)+n; return 0;}递归执行过程:最后往上依次返回值,5返回0,所以4返回0+20,所以3返回20+10,所以2返回30+5,所以结果就是35。二、不使用递归的写法:1234567891011121314151617#include <stdio.h>int g(int m,int n);int main(){ printf("g(3,5)的结果:%d\n",g(3,5)); return 0;}int g(int m,int n){ int sum=0; while(m--) { sum=sum+n; n=2*n; } return sum;}