回溯问题,可以递归解决,不断试探走一步,走两步
void go(int n,int step,int& sum){ n = n - step; if(n > 0) { go(n,1,sum); go(n,2,sum); } else if(n==0) { sum ++; }}int main(void) { int sum = 0; go(10,1,sum); go(10,2,sum); printf("sum=%d",sum); return 0;}
int foo(int n){ if(n < 0) return 0; if(n == 1 || n ==0) return 1; return foo(n-1) + foo(n-2);}