代码实践
#include <iostream>
using namespace std;
#include <vector>
const vector<unsigned int>* fib(int size)
{
static vector<unsigned int> nums;
if (size <= 0 || size >= 1024)
{
cerr << "size <= 0 || size >= 1024" << endl;
return 0;
}
for (int i = nums.size(); i < size; i++)
{
if (i == 0 || i == 1)
{
nums.push_back(1);
}
else
{
nums.push_back(nums[i - 2] + nums[i - 1]);
}
}
return &nums;
}
void Print(const vector<unsigned int>* fib)
{
if (!fib)
{
cerr << "Error: !fib" << endl;
return;
}
for (int i = 0; i < (*fib).size(); i++)
{
cout << (*fib)[i] << ' ';
}
cout << endl;
}
int main()
{
const vector<unsigned int>* v = fib(1000);
Print(v);
system("pause");
return 0;
}
今天是20200303 写代码好有讲究啊!正规、大气的编程风格不简单!