[原创]f(m,n)
利用堆栈写下列的算法:
当m*n=0时, f(m,n)=m+n+1;
当m*n!=0时,f(m,n)=f(m-1,f(m,n-1));
#include <stdio.h>
int f(int m,int n) { /*递归形式*/
if(m*n) return f(m-1,f(m,n-1));
return m+n+1;
}
int fd(int m,int n) { /*非递归形式(用while-if形式实现)*/
int s[5000],i=0;
while(m*n||i) {
if(m*n) s[i++]=m,--n;
else if(i) n=m+n+1,m=s[--i]-1;
}
return m+n+1;
}
main() {
printf("f:%d,fd:%d\n",f(3,4),fd(3,4));
}