学号:2020010944
1.采用特征方程方法求解以下递归方程:
H
(0)=0
H
(1)=1
H
(2)=2
H
(
n
)=
H
(
n
-
1)+9
H
(
n
-
2)
-
9
H
(
n
-
3)
当
n
>2
解:
2. 分析求斐波那契 f(n)的时间复杂度。
3.对于一个采用字符数组存放的字符串 str,设计一个递归算法求其字符个数(长
度)
思路:定义一个变量length,用来计数字符个数; 2.如果str等于null,返回length; 3.否则,length加1,str指向下一个字符; 4.递归调用步骤2和3,直至str等于null,返回length。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
char s[N];
int Length(char *s)
{
// cout << s << endl;
if(*s == '\0') return 0;
else return Length(s + 1) + 1;
}
int main()
{
cin >> s;
int num = Length(s);
cout << num << endl;
return 0;
}
运行结果如下: