- #include <iostream>
- using namespace std;
- static int steps;
- void count_steps(int m) //全局计数法
- {
- if(m == 0) {steps+=0;return;}
- if(m == 1) {steps+=1;return;}
- else if(m == 2) {steps+=2;return;}
- else
- {
- count_steps(m-1);//首步跳一步
- count_steps(m-2);//首步跳两步
- }
- }
- int jump(int m) //返回值法
- {
- if(m==0) return 0;
- if(m==1) return 1;
- if(m==2) return 2;
- else if(m>2)
- return jump(m-1)+jump(m-2);
- }
- void main()
- {
- int n;
- steps = 0;
- cout<<"input the steps number:";
- cin>>n;
- count_steps(n);
- cout<<"the total number:"<<steps<<endl;
- int a=jump(n);
- cout<<"the total number:"<<a<<endl;
- }
- //该片段来自于http://www.codesnippet.cn/detail/060620149732.html
来源: http://www.codesnippet.cn/detail/060620149732.html