- 论坛徽章:
- 0
|
母牛数量算法
- #include <deque>;
- #include <cstdlib>;
- #include <iostream>;
- using namespace std;
- int
- main(int argc, char** argv)
- {
- if(argc != 2)
- {
- cout << "just accept 1 parameter\n";
- exit(1);
- }
- int year = atoi(argv[1]);
- deque<long long>; count(4, 1);
- count.pop_back();
- for ( int i = 2; i < year; i ++)
- {
- count.push_back(count[0] + count[2]);
- count.pop_front();
- }
- cout << "In the " << year <<"th year, there are(is) "
- << count[1] << " cows\n";
- }
复制代码
同样是n级别的时间复杂度,但是空间复杂度变为1,deque的空间只有4.
不知道这个在时间上跟上一个有什么区别,不过估计也很难反映出来测试到117时就溢出了。 |
|