c2_main.cpp
来自「essential c++ 书上代码和习题答案」· C++ 代码 · 共 46 行
CPP
46 行
/**************************************************
* Essential C++ -- Stanley Lippman
* Addison-Wesley
* ISBN 0-201-48518-4
* homepage: www.objectwrite.com
* email: slippman@objectwrite.com
*************************************************/
#include <iostream>
using namespace std;
bool fibon_elem( int pos, int &elem )
{
if ( pos <= 0 || pos > 1024 )
{
cerr << "invalid position: " << pos
<< " -- cannot handle request!\n";
elem = 0;
return false;
}
elem = 1;
int n_2 = 1, n_1 = 1;
for ( int ix = 3; ix <= pos; ++ix )
{
elem = n_2 + n_1;
n_2 = n_1; n_1 = elem;
}
return true;
}
bool print_sequence( int pos )
{
if ( pos <= 0 || pos > 1024 ){
cerr << "invalid position: " << pos
<< " -- cannot handle request!\n";
return false;
}
cout << "The Fibonacci Sequence for "
<< pos << " positions: \n\t";
// prints
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?