1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1017 B
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

class Fib
{
private:
int nth; //项数
int value; //斐波那契数列第n项的值
public:
Fib(int n = 0, int v = 0); //构造函数初始化数据成员nth和value的值。nth和value的缺省值分别为0和0。
~Fib(){}; //析构函数
int &GetN(int &n) const; //获得私有数据成员nth的值
int GetValue() const; //获得私有数据成员value的值
void Value(const int &n); //计算并设置斐波那契数列第n项的值value
};
Fib::Fib(int n, int v) //构造函数初始化数据成员nth和value的值
{
nth = n;
value = v;
}
int &Fib::GetN(int &n) const //获得私有数据成员nth的值并将nth的值返回给实际参数。函数的返回值是引用。
{
n = nth;
return n;
}
int Fib::GetValue() const //获得私有数据成员value的值
{
return value;
}
void Fib::Value(const int &n) //计算并设置斐波那契数列第n项的值value
{
int a = 0, b = 1;
for (int i = 0; i < n; i++)
{
int t = a + b;
a = b, b = t;
}
value = a;
//请在这里添加你hw1_3的代码。并在你的readme.txt文件中分析Value()函数的时间复杂度。
}