学完了栈,对递归的理解深入了好多。
递归要注意两点:
1.递归终止条件,这也决定了递归必定是带有选择结构
2.递归函数得以返回。
20元汽水问题,给出我的解法:
#include <stdio.h>
int get(int g,int p,int n);
int main(){
int e;
e=get(20,20,20);
printf("%d",e);
return 0;
}
int get(int g,int p,int n){
if(p/2+g/3<1) return n;//当前一瓶水都换不了就结束。
return get((g%3+g/3+p/2),(p%2+p/2+g/3),(n+g/3+p/2));
}